Store: Router plugin: Flatten the router params

Created on 18 Aug 2018  路  2Comments  路  Source: ngxs/store

Feature Request

Versions

* ngxs: 3.2.0
* @angular/core: 6.1.2

Since we are moving most of the logic to the store and we have the router state integrated in the root state, we should consider accessing the route params from within the actions, something like this

```ts
@Action(ViewUser)
onViewUser(ctx: StateContext) {

const uid = this.store.selectSnapshot(state => state.router.params['id']);

const collection = this.store.selectSnapshot(state => state.users.collection.data);

// Check if user already exists in the collection
if (collection[uid]) {

  ctx.patchState({
    user: collection[uid],
    error: null
  });
} else {
  // Otherwise fetch the user from the database
  return ctx.dispatch(new GetUser(uid));
}

}

### Observed behavior

Currently the route params is located very deep

```ts
const uid = this.store.selectSnapshot(state => state.router.state.root.firstChild.firstChild.params['id']);
// or
const uid = this.store.selectSnapshot(state => state.router.state.root.children[0].children[0].params['id']);

Desired behavior

const uid = this.store.selectSnapshot(state => state.router.params['id']);
// or
const uid = this.store.selectSnapshot(state => state.router.state.root.params['id']);

It would be a great if we can refactor the router state and make it more friendly and flattened

Most helpful comment

You can write your own custom router serializer, just like the new example in the docs:
https://ngxs.gitbook.io/ngxs/plugins/router#custom-router-state-serializer

All 2 comments

You can write your own custom router serializer, just like the new example in the docs:
https://ngxs.gitbook.io/ngxs/plugins/router#custom-router-state-serializer

@eranshmil This is exactly what I was looking for!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

markwhitfeld picture markwhitfeld  路  24Comments

amjmhs picture amjmhs  路  21Comments

armanozak picture armanozak  路  23Comments

zygimantas picture zygimantas  路  32Comments

markwhitfeld picture markwhitfeld  路  41Comments