Store: Resolved data not in store

Created on 5 Jul 2018  路  13Comments  路  Source: ngxs/store

I'm using current version of ngxs with router plugin.

I've created my own router serializer:

import {RouterStateSerializer} from '@ngxs/router-plugin/src/serializer';
import {ActivatedRouteSnapshot, Params, RouterStateSnapshot} from '@angular/router';

export interface RouterStateParsed {
  url: string;
  queryParams: Params;
  params: Params;
  data: any;
}

export class AppRouterSerializer implements RouterStateSerializer<RouterStateParsed> {
  serialize(routerState: RouterStateSnapshot): RouterStateParsed {
    const {url} = routerState;
    const {queryParams} = routerState.root;

    let state: ActivatedRouteSnapshot = routerState.root;
    while (state.firstChild) {
      state = state.firstChild;
    }
    const {params, data} = state;

    return {url, queryParams, params, data};
  }
}

I think that serialize is triggered before router resolver is resolved and that's why I don't have resolved data in store.

Is it a bug?

plugins

Most helpful comment

I don't think the current behavior would be desirable in any case, as one expects their resolved data to show up in the state. That's the case for me, at least.

It would be opening doors rather than closing them

All 13 comments

@leon - Any thoughts? You wrote this one.

@leon any news on that one?

Any news? This is a pretty vital feature.

Any news? This is a pretty vital feature.

Could you create a repro stackblitz using the following template?
https://stackblitz.com/fork/ngxs-simple

Any news? This is a pretty vital feature.

Could you create a repro stackblitz using the following template?
https://stackblitz.com/fork/ngxs-simple

sure thing
https://stackblitz.com/edit/ngxs-router-resolver

@piernik and @hallowatcher do you think that the current behavior would be desirable in any circumstance? We have a fix ready in PR #895 but just want to make sure that we don't need to provide a way of falling back to the current behavior.

I don't think the current behavior would be desirable in any case, as one expects their resolved data to show up in the state. That's the case for me, at least.

It would be opening doors rather than closing them

I agreed with @hallowatcher In store should be up-to-date state.

The fix has been released in v3.4.3!
Please test and confirm that it is working as expected.

Yes! It's working as expected.

I updated the stackblitz to use the new version and it's working
https://stackblitz.com/edit/ngxs-router-resolver

I've updated Your lib and I think it still is not working as expected (from my point of view).
Now resolved data is in page component. But now up-to-date data is not available in resolver :/
In resolver we have data from previous state. Was with current data so I could use it in resolver. Can't You make it always up-to-date?

I think store should be updated twice - before and after resolving so that data will be always up-to-date as in angular router.

Do you have any use-cases for what you described?

While it's true that you don't have the updated store in a resolver, all a resolver (usually) does is fetch data from a service with, for example, an ID in query params.

A resolver is like a service and is imported as such, so I don't see why you would need to get the ID from the store instead of the router.

So I do agree that such feature would be nice, but not critical.

I've made na CRM system that uses selectors which are connected to ngxs store. And I prefer using selectors all over my code instead of direct Store or ActivatedRoute. This way I can change data provider easily.

So in my resolvers or guards I'm using those selectors to fetch data set in router definition.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TomDemulierChevret picture TomDemulierChevret  路  3Comments

eranshmil picture eranshmil  路  5Comments

malfusion picture malfusion  路  4Comments

paulstelzer picture paulstelzer  路  5Comments

ToxicToast picture ToxicToast  路  6Comments