代码之家  ›  专栏  ›  技术社区  ›  Boris

NGRX 8效果无限期发射

  •  0
  • Boris  · 技术社区  · 6 年前

    我是新来的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) {
      }
    
    }
    
    

    我做错了什么?

    1 回复  |  直到 6 年前
        1
  •  0
  •   robert    6 年前

    您需要添加此项: { dispatch: false }

    here .