2016-12-13 14 views
1

我有一个与我的组件有关的init数据的商店,这个数据的一个例子是一个字符串与apiPath。如何等待,直到一个商店被执行后,分配一个动作dispach第二个动作

ngOnInit() { 
     // this store contains data related to the component 
     this.store.dispatch(this.searchActions.addPageData(this.pageData)); 
    } 

然后我从以前的存储需要的数据和被触发每当有一个路线PARAM变化另一个动作:

this.route.params.subscribe((params: Params) => { 
    this.store.dispatch(this.searchActions.routerParamsSearchPageChanged(pageFilters)); 
}) 

你怎么等到第一个行动,以填补商店,派遣第二个动作。

+0

http://stackoverflow.com/questions/35173649/angular2-how-to-chain-async-service-calls-http-requests-in-a-component – silentsod

+1

我想这可能与:HTTP ://stackoverflow.com/questions/40872357/waiting-for-ngrx-action-before-loading-page-with-url-parameter/40905330 – chrigu

+0

http://stackoverflow.com/questions/40343835/performing-advanced-http -requests-in-rxjs/40346998,http://stackoverflow.com/questions/41092488/rxjs-json-data-with-an-array-processing-each-item-further-in-the-stream/41096657,http ://stackoverflow.com/questions/39566268/angular-2-rxjs-how-return-stream-of-objects-fetched-with-several-subsequent/39578646#39578646 – martin

回答

1

我假设第一个动作(addPageData)用一些信息填充商店。然后,你想用路由参数过滤掉数据,猜测某种类型的查询?如果这个查询不适合你的需要,也许你可以详细说明一下。

基本上,检索你想要的商店信息和查询参数。按照某种标准过滤该查询,这可能只是像我一样检查null以确保数据存在。

Observable.combineLatest(
     this.store.select(state => state.bookState), 
     this.route.params.select<string>('query') 
    ) 
    .filter(([bookState]) => bookState.books !== null) 
    .subscribe(([books, query]) => { 
     this.store.dispatch({ 
      type: '[Books] FILTER_BY_QUERY', 
      payload: { 
       bookType: books.type, 
       query: query 
      } 
     }); 
    }); 
相关问题