Dva: models中effects中多个方法如何调用他们之间都公用的方法?

Created on 9 May 2017  ·  4Comments  ·  Source: dvajs/dva

effects中是saga的控制流,当多个saga需要调用公共的方法,可以抽取到services中调用,但是有些公共方法不是services的方法,经过测试在effects中定义的方法是不能相互调用,比如openView无法调用common1和common2,这是为什么?对于公共方法的场景应该如何解决?谢谢!

effects : {
   *openView(){
      const returnValue =yield common1();
      yield common2();
   }

  //非saga的方法,而是saga中需要共用的方法
  *common1(){
     return "****";
  }

  *common2(){
  }

}

Most helpful comment

put({type: 'common1'})

All 4 comments

put({type: 'common1'})

@nihgwu 对于common1()有返回值的函数了?

看你common1的定位啊,如果他不是个saga,只是一个通用函数,直接写在外面,如果是saga,要么用state传递,要么就用都可以访问的局部变量

最近也遇到了这个问题,多个effect互相调用,并且需要接收返回值。
如果不需要接受返回值的话,完全可以 yield put({ type: 'typeName', payload })就能解决,但是如果接收结果的话,需要结合putcall来构造一个函数解决,代码如下:

      const anotherAction = yield put({ type: 'typeName' , payload })
      const anotherResult = yield call(() => 
        anotherAction.then(actionResultData => 
           Promise.resolve(actionResultData)
        )
      )
      console.log(anotherResult )

函数里边也可以加入异常处理完善逻辑

Was this page helpful?
0 / 5 - 0 ratings