2016-09-29 22 views
0

我正在使用ngrx。我得到了錯誤無法讀取未定義的屬性'type'(ngrx)

無法讀取的不確定

屬性「類型」這是我的代碼部分:

@Effect() foo$ = this.actions$ 
    .ofType(Actions.FOO) 
    .withLatestFrom(this.store, (action, state) => ({ action, state })) 
    .map(({ action, state }) => { 
     if (state.foo.isCool) { 
     return { type: Actions.BAR }; 
     } 
    }); 

回答

3

這個問題有點棘手,因爲它是不容易找到基於錯誤的問題。

在這種情況下,當state.foo.isCoolfalse時,不返回任何操作。

所以要改變這樣的事情會解決這一問題:

@Effect() foo$ = this.actions$ 
    .ofType(Actions.FOO) 
    .withLatestFrom(this.store, (action, state) => ({ action, state })) 
    .map(({ action, state }) => { 
     if (state.foo.isCool) { 
     return { type: Actions.BAR }; 
     } 

     return { type: Actions.SOMETHING_ELSE }; 
    }); 
+0

我觀察,很多時候你自己問的問題和答案,這只是5分鐘之內。 – micronyks

+0

@micronyks是啊,你可以回答你自己的問題,當你問。 http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/ –

+0

@micronyks我看到很多人在使用ngrx時遇到這個問題。我想我最好在這裏提供一個解決方案,而不是一次又一次幫助他們分別花費額外的時間。 –

相關問題