我是NGRX的新手。我试图创建一个简单的测试动作,其效果只是将123打印到控制台,但是当我打开浏览器时,我看到此效果会无限期触发(一次又一次打印123)。
这是我的代码(是的,我将所有逻辑都放在一个文件中,仅用于测试目的):
import {Component, Injectable, OnInit} from '@angular/core';
import {createAction, createReducer, on, Store} from '@ngrx/store';
import {Actions, createEffect, ofType} from '@ngrx/effects';
import {tap} from 'rxjs/operators';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
constructor(private store: Store<AppState>) {
}
ngOnInit() {
this.store.dispatch(test());
}
}
interface AppState {
}
const test = createAction('[Test] Test');
export const reducer = createReducer(
{},
on(test, state => state)
);
@Injectable()
export class Effects {
test$ = createEffect(() => this.actions$.pipe(
ofType(test),
tap(() => console.log(123))
));
constructor(private actions$: Actions) {
}
}
我做错了什么?