Dva: 跨model的通信 并没有官网描述预期的效果

Created on 27 Dec 2018  ·  12Comments  ·  Source: dvajs/dva

Code to reproduce the issue: (请提供可复现的代码或者步骤)

官网脚手架初始化项目后, 添加

//namespace page1
// ui 中触发 dispatch({type: "page1/p1"})
effects: {
    *p1({ payload }, { call, put }) {  // eslint-disable-line
      yield call({ type: 'page2/p1'})
    },
  },
//model page2
effects: {
    *p1({ payload }, { call, put }) {  // eslint-disable-line
         yield something
    }
}

Expected behavior: (预期的正常效果)

官网文档:

父容器A,子容器B,二者各自connect了不同的model A和B
父容器中有一个操作,分三个步骤:
model A中某个effect处理第一步
call model B中的某个effect去处理第二步
第二步结束后,再返回model A中做第三步
在dva中,可以用namespace去指定接受action的model,所以可以通过类似这样的方式去组合:

yield call({ type: 'a/foo' });
yield call({ type: 'b/foo' });
yield call({ type: 'a/bar' });

Actual behavior: (实际效果)

uncaught at check call: argument [object Object] is not a function

报错显示 call的参数不是function
这个 call被改造过吗? 不可能直接支持 参数是对象的吧, 官网中说的到底是什么意思
代码为什么没有官网文档中预期效果?

Versions of packages used: (哪个库的哪个版本出现的问题)

最新装的cli 初始化的项目

wontfix

Most helpful comment

see: https://dvajs.com/guide/develop-complex-spa.html#%E8%B7%A8model%E7%9A%84%E9%80%9A%E4%BF%A1

是文章写错了。

也可能只是想表达某种简略写法;但很容易误导人。

如果你是想阻塞地“执行”另一个 model 中的 effect 函数,从 v2.3.0 开始可以用 put.resolve

const result = yield put.resolve({ type: 'someActionType', payload })
// result 会是该 effect 生成器函数中 return 的值

All 12 comments

亲测,yield put({ type: 'a/foo' }); 是可以的

亲测,yield put({ type: 'a/foo' }); 是可以的
@JxSx

put 是可以;
yield call({ type: 'a/foo' });你试一下call 按照官网的 示例来

dva/packages/dva-core/src/getSaga.js

Line 141 in 5f77c45

return { ...sagaEffects, put, take };

貌似并没有重新写call

@951565664
具体代码 没有去看 ,也猜测是call 没有重写,做个伸手党, 等官方来确认下。暂时是用别的方法绕过了

@meooxx 我记得以前的call 是有重写的。我之前一直都是用的put来实现的。我猜测可能文档写错了

@meooxx 我记得以前的call 是有重写的。我之前一直都是用的put来实现的。我猜测可能文档写错了

问个不相关的, 你那个 引用代码是怎么引用过来的

@951565664 👍学会了

亲测,yield put({ type: 'a/foo' }); 是可以的
@JxSx

put 是可以;
yield call({ type: 'a/foo' });你试一下call 按照官网的 示例来

@meooxx 等一下,call( action ) 是个什么用法?这里 action 是个纯对象,你对它用 call,根本不能生成有效的 saga effect 啊。

亲测,yield put({ type: 'a/foo' }); 是可以的
@JxSx

put 是可以;
yield call({ type: 'a/foo' });你试一下call 按照官网的 示例来

@meooxx 等一下,call( action ) 是个什么用法?这里 action 是个纯对象,你对它用 call,根本不能生成有效的 saga effect 啊。

see: https://dvajs.com/guide/develop-complex-spa.html#%E8%B7%A8model%E7%9A%84%E9%80%9A%E4%BF%A1

see: https://dvajs.com/guide/develop-complex-spa.html#%E8%B7%A8model%E7%9A%84%E9%80%9A%E4%BF%A1

是文章写错了。

也可能只是想表达某种简略写法;但很容易误导人。

如果你是想阻塞地“执行”另一个 model 中的 effect 函数,从 v2.3.0 开始可以用 put.resolve

const result = yield put.resolve({ type: 'someActionType', payload })
// result 会是该 effect 生成器函数中 return 的值

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings