Dva: 怎么在组件外部获取store?

Created on 28 Jan 2018  ·  11Comments  ·  Source: dvajs/dva

比如: 我想在request.js中拿到store中的token, 应该怎么做?
我看redux文档是这么写的:

import { createStore } from 'redux'
let store = createStore(todoApp)

可是dva创建的项目中, 这里的store好像并没有经过我手...

question

Most helpful comment

可以通过app._store方式获取

All 11 comments

request.js 是从 effect 调的,effect 可以拿到 state,然后传给 request.js。

那岂不是每次请求都要取一次token, 能不能在request.js里拿到token自动加上

不能,request.js 里不能直接取 state 。

@bowencool 你这种需求可以创建一个全局对象,自己来维护,需要管理的数据就是数据量小,但是使用频繁的。

@yvanwangl 多谢提醒

model中,effects中,怎么调用state呢???

@xiaofengqqcom123 select

可以通过app._store方式获取

这样获取呢,多看文档哈。
window.g_app._store
window.g_app._store.dispatch

文档地址:
https://umijs.org/zh/guide/with-dva.html#%E9%85%8D%E7%BD%AE%E5%8F%8A%E6%8F%92%E4%BB%B6

https://umijs.org/plugins/plugin-dva#getdvaapp

用 getDvaApp() 来获取

getDvaApp()._store

Was this page helpful?
0 / 5 - 0 ratings