Store: 2 Lazy Features only for the first loaded feature the State is initialized

Created on 29 Mar 2018  路  9Comments  路  Source: ngxs/store

We have in our real project 3 feature only the first we start works correctly.
The problem is, that the state of the next called feature were not initlized.

I rebuild the problem at stackblitz.io

What do we wrong?

Versions

* ngxs: 2.0.0-rc.24
* @angular/core: 5.2.8

Repro steps

You can clear the application storage and begin with Lazy2Modul.
Than the Problem come an LazyModul

Observed behavior

the second called Lazy(x)State is not initialized

ng:///Lazy2Module/Lazy2Component.ngfactory.js:8 ERROR TypeError: Cannot read property 'count' of undefined
    at Lazy2State.count (lazy2.state.ts:36)
    at MapSubscriber.fn_1 (selector.ts:16)
    at MapSubscriber.memoized [as project] (memoize.ts:13)
    at MapSubscriber._next (map.ts:78)
    at MapSubscriber.Subscriber.next (Subscriber.ts:96)
    at StateStream.BehaviorSubject._subscribe (BehaviorSubject.ts:22)
    at StateStream.Observable._trySubscribe (Observable.ts:217)
    at StateStream.Subject._trySubscribe (Subject.ts:106)
    at StateStream.Observable.subscribe (Observable.ts:202)
    at MapOperator.call (map.ts:53)

Desired behavior

both feature should be working and have separated counters

Mention any other details that might be useful (optional)

Here is the code: https://stackblitz.com/edit/ngxs-localstorage-more-lazys?file=app%2Fapp.module.ts

Most helpful comment

All 9 comments

I was able to get it working after updating to the 2.0.0 and changing some stuff.

https://stackblitz.com/edit/ngxs-localstorage-more-lazys-nncku1

All Action class names must be unique within the app.
Since we use the name of the class as the unique identifier when dispatching and handling them.

Sorry for taking so long time! Thx for your fast answer!
You have deavtivated the storageplugin
--> NgxsStoragePluginModule.forRoot()
When i do this in my real world project, it solves the problem, that i described in this issue.
But we actually planed to use the storage plugin. We want to keep the state in the app.storage.

But i looks like, the problem is about handling stages in the storage with more than one lazy feature

It seems that NgxsStoragePluginModule currently doesn't work with lazy loading. Only root states will be initialized. (3.0.0.beta)

I found a workaround thats works for me.
I changed the example for showing it:
https://stackblitz.com/edit/ngxs-localstorage-more-lazys-sae5sn?file=app%2Flazy2%2Flazy2.state.ts

I just set a fallback-value when i call getState:
const state = sc.getState() || initialLazy2State;
or
const state = sc.getState() || initialLazyState;

This works for me. I did not find the right place to do that in the framework itself. So i could not deliver a fork with a pull request.

@svenjeppson thanks for posting this, works well for me as I had encountered the same issue.

I'm seeing more issues with the NgxsStoragePluginModule even when I use @svenjeppsson 's workaround. I forked the stackblitz project above and added a root app state. One of the lazy modules works, the other throws errors in the console...

Error: undefined is not an object (evaluating 'local[k] = val[k]')

I just updated everything in the stackblitz demo to latest and I'm still seeing the issue.

screen shot 2018-04-27 at 9 01 24 am

Was this page helpful?
0 / 5 - 0 ratings