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

[vue] 请求数据是写在组件的methods中还是在vuex的action中? #392

Open
haizhilin2013 opened this issue Jun 20, 2019 · 9 comments
Labels
vue vue

Comments

@haizhilin2013
Copy link
Collaborator

[vue] 请求数据是写在组件的methods中还是在vuex的action中?

@haizhilin2013 haizhilin2013 added the vue vue label Jun 20, 2019
@wenyejie
Copy link

vuex是状态管理, 而不是用来请求数据.
反正我是这么理解的.

@suchanglin
Copy link

组件吧,数据就近维护。不是所有数据都需要放在state。

@April-Zheng
Copy link

组件吧 vuex是状态管理

@miqidian
Copy link

miqidian commented Aug 7, 2019

如果请求的数据是多个组件共享的,为了方便只写一份,就写vuex里面,如果是组件独用的就写在当前组件里面。

@soakit
Copy link

soakit commented Aug 28, 2019

写vuex的action中,统一管理。
另外个人觉得在组件中this.$store.dispatch('xxx')没有使用mapActions辅助函数将组件的methods映射为store.dispatch调用来得优雅

@zhengwei1949
Copy link

写在actions中,为了复用

@xyq0716
Copy link

xyq0716 commented Nov 22, 2019

个人认为如果请求回来的数据如果是多次在多个组件中共用的话,就写在vuex这个统一数据管理仓库中,并放在vuex的actions属性中定义请求函数(请求是异步的,不能放在mutations中),然后在用到请求数据的地方通过$this.dispatch(‘actions名称’)去触发执行请求。

但是如果请求只在该组件用到的话就没必要放在vuex中了,直接在该组件的methods中请求即可。

@ajh99990
Copy link

ajh99990 commented Dec 3, 2019

莫名其妙的问题,强行凑数的? 数据需要在vuex中维护就在actions里面请求,否则在组件内请求。具体情况具体分析。

@WenJieLi1998
Copy link

根据业务场景划分,如果该请求数据的方法是多个视图共享的话,则写在action中,如果是当前视图所用,则写在组件的methods中

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

No branches or pull requests

10 participants