Ways to update a route's resolved information

Look at the route setup below:

const routes: Routes = [
  {
    path: '',
    component: AppComponent,
    resolve: {
      app: AppResolver
    },
    children: [
      {
        path: 'user/:uid',
        resolve: {
          user: UserResolver
        },
        children: [
          {
            path: '',
            component: ViewUserComponent
          },
          {
            path: 'edit',
            component: EditUserComponent
          }
        ]
      }
    ]
  }
];

The user information is fetched using UserResolver for any child routes of /user/:uid. This allows users to view a profile by going to /user/:uid and then edit it by visiting /user/:uid/edit. After editing, one would expect to see the updated profile (/user/:uid).

However, since both the view and edit pages are children of the resolved route, the resolver doesn't trigger when moving between them.

As a result, returning from the edit page to the view page still shows the old data.

Is there a way to refresh the resolved user data to fetch it again from the server? Alternatively, is there another solution to achieve this outcome?

Since the app data remains unchanged, reloading should not be necessary.

Answer №1

The 'runGuardsAndResolvers' solution has been tested and successfully implemented in my specific case. The resolver now gets called every time there is a sub-page change.

To achieve this behavior, you just need to add the option to the relevant parts of your routes.

export const ROUTES: any = [
  {
    path: 'project/:name',
    runGuardsAndResolvers: "always",
    component: ProjectComponent,
    resolve: { project: ProjectResolver },
    children: [
      ...

Answer №2

If you're configuring your routes, don't forget about the 'runGuardsAndResolvers' parameter. Setting it to 'always' ensures that the resolver will run every time the child routes of that specific route are changed. Check out the route documentation for additional details.

Answer №3

Here is a possible approach:

constructor(private route: ActivatedRoute) {
  this.route.paramMap.subscribe(params => {
    this.info = params.get('info');
  });
}

updateInfo(newInfo) {
  (this.route.parent.data as BehaviorSubject<any>).next({info: newInfo});
}

Answer №4

Utilizing the aforementioned method allows me to invoke the resolver. However, it does not alter the data fetched from my backend server. To address this issue, I have implemented a workaround by calling my service method once more within the success segment, retrieving updated data from the backend server.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ensure that the types of objects created by spreading are checked

When we spread two different types of objects to compose a new object in TypeScript, the type-checking is not automatically enforced. So how can we make sure that TypeScript performs the necessary checking? type TypeA = { id: number; } type TypeB = { ...

Issue encountered when trying to use Array.sort() method to sort an array of objects

I'm facing an issue sorting an array of objects by a name property present on each object. When utilizing the sort() method with the given code snippet, I encounter the following error: ERROR ReferenceError: b is not defined This is my code block: m ...

Executing an Angular2 Function in Unity WebGL

Currently, I am utilizing Angular2 version 2.1.2 along with a Unity visualizer created with Unity 5.5. My main requirement is to establish communication from Unity to Angular2. I had previously used a code snippet similar to the one below public void G ...

Unable to modify the text value of the input field

I am currently learning how to code in React, so please forgive me if my question seems basic. I am attempting to change the text of an Input element based on the value of the filtered variable, like this: const contactContext = useContext(ContactContext); ...

What is the best approach to implementing role-based authentication within a MEAN application?

Currently, I am developing a mean stack application and looking to implement role-based authentication. For instance, if the user is an admin, they should have additional permissions and access rights. Any guidance on implementing this feature would be g ...

When setting up Webpack with TypeScript, an error is encountered related to imports

Recently, I attempted to convert my Webpack configuration from JavaScript to TypeScript but encountered numerous difficulties in the process. To kick things off, I created a basic webpack configuration file with some parts missing. Here is how my webpack.c ...

"Encountering a failure in an Angular test when attempting to execute an

The content below shows my dashboard.component.ts file: import { Post } from "./../models/post"; import { Component, OnInit } from "@angular/core"; @Component({ selector: "app-dashboard", templateUrl: "./dashboard.component.html", styleUrls: ["./da ...

Easy-to-use blog featuring Next.js 13 and app router - MDX or other options available

Currently in the process of transitioning a Gatsby website to Next.js (v13.4) and utilizing the new App Router. However, I'm facing some challenges when it comes to migrating the blog section over because there isn't much accurate guidance availa ...

Node's TypeScript parser loses the order of same name-tags when converting XML to JSON

I've experimented with xml2js and fast-xml-parser and received similar results from both (in different formats, but that's not the focus here) This specific example is from fast-xml-parser Here's the XML data: <test version="1" ...

Exploring the capabilities of ngx-img-zoom with Angular using an HTML range slider

Implementing Angular ngx-img-zoom with simultaneous pinch-zoom and scroll zoom https://i.sstatic.net/mT8WQ.gif ...

When trying to import a module in Angular, an error is encountered while using Scully

Exploring the utilization of Scully within my Angular project has presented me with a challenge. In Angular, Modules must be imported in order to use them effectively. However, when attempting to execute Scully through the command line: npm run scully --sc ...

Using Apache to rewrite wildcard subdomains for an Angular application

I am currently facing a challenge with my Angular app that needs to be set up under subdomains for multi-tenant support. Each subdomain represents a different tenant within the application. At present, I have an .htaccess configuration that successfully lo ...

Is there a way to retrieve the id attribute of an option element from the source component?

When a user makes a selection, I am trying to access the id attribute of the HTMLOptionElement. However, it always returns 0 instead of the actual id I passed (such as 1 or 2) from the select tag: <div class="col-8"> <select (cha ...

Transform the data received from the server into a TypeScript object

I'm facing a challenge in converting an object retrieved from the server to a TypeScript class I've created. While my TypeScript object has identical properties to the one returned by the server, the difference lies in the casing of the property ...

Using constants is a great way to streamline the process of displaying labels in a user interface built with Angular

Is there a way to dynamically display labels on my web page using constants, properties, or a JSON file in Angular 7? The goal is to allow for easy updates of label text without directly changing the HTML files. Instead, I want to be able to update a cons ...

Typescript is throwing an error stating that the module does not have any exported

Currently, I am using TypeScript for a personal project and attempting to import a function from a library called Solid Client JS. The issue arises when I only include the following line in my single `file.ts`: import { getFile } from '@inrupt/solid- ...

Utilize ngFor in Angular Ionic to dynamically highlight rows based on specific criteria

I'm working on an application where I need to highlight rows based on a count value using ngFor in Angular. After trying different approaches, I was only able to highlight the specific row based on my count. Can someone please assist me? Check out m ...

Refresh Form Following Submission

When using a react form that triggers a graphql mutation upon button click, the text entered in the form fields remains even after the mutation has been executed. This necessitates manual deletion of text for subsequent mutations to be run. Is there a way ...

How can LDAP authentication be implemented in an Angular 8 frontend application?

I need to create a web application with secure content access. I've been advised to implement LDAP authentication on the front-end using Angular. I'm uncertain about its feasibility as I couldn't find relevant information online. Can anyone ...

Vue.js - Prop validation error: The prop "page" expects a value of type DocumentPageDto, but received an Object instead

My component PageViewer has props defined like this: props: { page: DocumentPageDto, }, I'm using it in another Vue component like this: <div><PageViewer :page="selectedPage"></PageViewer></div> The value of ...