Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

怎么在组件外部获取store? #1491

Closed
bowencool opened this issue Jan 28, 2018 · 11 comments
Closed

怎么在组件外部获取store? #1491

bowencool opened this issue Jan 28, 2018 · 11 comments
Labels

Comments

@bowencool
Copy link

bowencool commented Jan 28, 2018

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

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

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

@sorrycc
Copy link
Member

sorrycc commented Jan 28, 2018

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

@bowencool
Copy link
Author

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

@sorrycc sorrycc closed this as completed Jan 28, 2018
@sorrycc
Copy link
Member

sorrycc commented Jan 28, 2018

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

@yvanwangl
Copy link

yvanwangl commented Jan 29, 2018

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

@bowencool
Copy link
Author

@yvanwangl 多谢提醒

@xiaofengqqcom123
Copy link

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

@nihgwu
Copy link
Member

nihgwu commented Apr 5, 2018

@xiaofengqqcom123 select

@nelhu
Copy link

nelhu commented Jan 9, 2019

可以通过app._store方式获取

@JimmyLv
Copy link

JimmyLv commented Apr 21, 2019

ref: Dva 源码解析 | DvaJS

@ghost
Copy link

ghost commented Oct 12, 2019

这样获取呢,多看文档哈。
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

@jnoodle
Copy link

jnoodle commented Jun 9, 2020

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

用 getDvaApp() 来获取

getDvaApp()._store

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants