简体   繁体   English

在一个 NgRx 中组合效果

[英]combine effects in one NgRx

I have similar effects and want to combine it in one but dont know how to do it我有类似的效果,想把它合二为一,但不知道怎么做

Here's my effects (I need to pass different id depends on action type):这是我的效果(我需要传递不同的id取决于动作类型):

setTopics$ = createEffect(
  () => this.actions$.pipe(
    ofType(setTopics),
    map((action) => 
      this.service.set(action.payload.id))),
  { dispatch: false });

setHeadlines$ = createEffect(
  () => this.actions$.pipe(
    ofType(setHeadlines),
    map((action) => 
      this.service.set(`ldb:${action.payload.id}:0`))),
  { dispatch: false });

I tried to combine actions in ofType like ofType(setTopics,setHeadlines) , but how to pass different ids in this.service.set()我尝试在ofType中组合操作,例如ofType(setTopics,setHeadlines) ,但是如何在this.service.set()中传递不同的 id

Would be really grateful for help!非常感谢您的帮助!

This can be achieved in two ways这可以通过两种方式实现

  1. if you want to keep the effect generic to just set what it receives, the set headlines action can set ldb:<id>:0 in payload as label and setTopics payload will set label same as id( or if you don't want label in setTopics you can check if label exists in effect then use label else id )如果您想保持效果通用以仅设置它接收的内容,则设置标题操作可以将有效负载中的ldb:<id>:0设置为 label 和 setTopics 有效负载将设置 label 与 id( 或者如果您不希望 ZD31415888在 setTopics 中,您可以检查 label 是否存在,然后使用 label 否则 id )
  2. If you don't care about it being generic, you can do something like this如果你不关心它是通用的,你可以做这样的事情
 if (action.type === actionsType.ACTION1) {
    value = action.payload.id
  } else if (action.type === actionsType.ACTION2) {
    value = `ldb:${action.payload.id}:0`
  }
 this.service.set(`ldb:${action.payload.id}:0`)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM