store: Action Handler not getting executed.
Versions
* ngxs: 3.0.0-dev.bcbc16f
* @angular/core: 6.0.0-rc.3
Repro steps
- Step 1 Added Injectable https://github.com/xmlking/nx-starter-kit/blob/master/apps/default/src/app/core/state/eventbus.ts
- Step 2 imported into Angular Module https://github.com/xmlking/nx-starter-kit/blob/master/apps/default/src/app/core/core.module.ts#L45
- Step 3 login to trigger “Login” action https://xmlking.github.io/nx-starter-kit/index.html
Observed behavior
no “New Login!..” log message printed
Desired behavior
expected to see “New Login!..” log in the console.
Mention any other details that might be useful (optional)
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 16 (10 by maintainers)
I’m having similar difficulties with action handlers. It seems it is not enough to just create the action handler and install it into the providers collection. You actually have to inject the action handler into something so it will be instantiated at least once by DI.
So I think the question here is… How does the action handler injectable get picked up by the system? It has to be injected into something at least once to get instantiated by DI. I put it on my constructor of my State class. Not sure if this is “correct” but it is working.
The action handler documentation needs to be enhanced telling us just how the injectable action handler is plugged into the system.
The router can’t be injected directly into your handler. Inject
Injector
. And then right before you need the router, use theInjector
to inject the router.“May be better to add it to foorRoot() and forFeature()?”
I was thinking the same thing. What I did to get it to work feels like a hack.
Maybe another parameter on the current forRoot or forFeature that takes an array of action handler types.
NgxsModule.forRoot([], [RouteHandler])
Since these feel like effects maybe a ActionHandlerModule with a forRoot/forFeature that takes an array of action handler types. But this feels like overkill as there is no real need for a ActionHandlerModule.
NgxsActionHandlerModule.forRoot([RouteHandler])