In previous applications using either Angular 8 and 9, I was using class-based actions rather than action creators. This time around, I decided to try using action creators instead. However, I've run into a hitch: where I previously used this.dataPersistence.navigation(...)
for async methods and success actions to execute based on navigation, it's now wrapped in createEffect(() => ...)
but it doesn't seem to work (whether it's wrapped or not)
Here's the setup and most of this is boilerplate:
package.json
"@nrwl/angular": "9.2.4",
...
"@angular/cli": "9.1.0",
"@nrwl/workspace": "9.2.4",
action.ts
export const setActivePanelId = createAction('[Interactions] Set Active Panel ID', props<{ id: string }>());
app.routes
const routes: Routes = [
{
path: '',
component: MessagesContainer
}
];
interactions.effects.ts
onNav$ = createEffect(() =>
this.dataPersistence.navigation(MessagesContainer, {
run: (a: ActivatedRouteSnapshot): Observable<Action> | Action | void => {
//An example callback, no async used yet.
return setActivePanelId({id: a['snapshot'].queryParams.panelId});
},
onError: (a: ActivatedRouteSnapshot, e: any): any => {
console.warn(e);
}
}));
app.module.ts
@NgModule({
declarations: [AppComponent, MessagesContainer],
imports: [
BrowserModule,
ComponentsModule,
RouterModule.forRoot(routes, routingOptions),
EffectsModule.forRoot([]),
NxModule.forRoot(),
StoreRouterConnectingModule.forRoot(),
StoreModule.forRoot({}, { metaReducers: [] }),
StoreDevtoolsModule.instrument({
maxAge: 20,
logOnly: config.environment.production
}),
InteractionsModule
],
bootstrap: [AppComponent],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
export class AppModule {
}
interactions.module.ts
@NgModule({
imports: [
CommonModule,
StoreModule.forFeature(
fromInteractions.INTERACTIONS_FEATURE_KEY,
fromInteractions.reducer
),
EffectsModule.forFeature([InteractionsEffects]),
StoreModule.forFeature(
fromInteractions.INTERACTIONS_FEATURE_KEY,
fromInteractions.reducer
)
],
providers: [InteractionsFacade]
})
export class InteractionsModule {}
UPDATE: I've also tried
@Effect() nav$ = createEffect(() =>
this.actions$.pipe(
// listens for the routerNavigation action from @ngrx/router-store
navigation(MessagesContainer, {
run: (activatedRouteSnapshot: ActivatedRouteSnapshot) => {
return setActivePanelId({id: 'async!'});
},
onError: (
activatedRouteSnapshot: ActivatedRouteSnapshot,
error: any
) => {
// we can log and error here and return null
// we can also navigate back
console.warn(error)
return null;
}
})
)
);