当Angular NGRX Effects没有发生任何事情时,可能有以下几种解决方法:
确保在你的应用程序的根模块中正确导入了Effects模块,并在EffectsModule.forRoot([])
中注册了所有的Effects。例如:
import { EffectsModule } from '@ngrx/effects';
@NgModule({
imports: [
EffectsModule.forRoot([MyEffects])
]
})
export class AppModule { }
在Effects类中,确保你正确导入了Actions
和ofType
操作符,并正确定义@Effect()
装饰器。例如:
import { Actions, ofType, Effect } from '@ngrx/effects';
@Injectable()
export class MyEffects {
constructor(private actions$: Actions) {}
@Effect()
myEffect$ = this.actions$.pipe(
ofType(MyActionTypes.SomeAction),
// 执行其他操作
);
}
在组件或其他地方触发Action之前,确保使用正确的Action类型,以及正确地分发了Action。例如:
import { Store } from '@ngrx/store';
import { myAction } from './my.actions';
export class MyComponent {
constructor(private store: Store) {}
someAction() {
this.store.dispatch(myAction());
}
}
在组件或其他地方,确保你正确订阅了Effects。例如,在构造函数中使用Effect.dispatch
方法:
import { Effect, Actions } from '@ngrx/effects';
@Injectable()
export class MyEffects {
constructor(private actions$: Actions) {}
@Effect({ dispatch: false })
myEffect$ = this.actions$.pipe(
ofType(MyActionTypes.SomeAction),
tap(() => {
console.log('Effect happened');
})
);
}
在这个例子中,Effect不会分发任何Action,但它将在控制台中打印一条消息。
通过检查以上内容,你应该能够找到导致Angular NGRX Effects不起作用的原因,并找到相应的解决方法。