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
Comments
vuex是状态管理, 而不是用来请求数据. |
组件吧,数据就近维护。不是所有数据都需要放在state。 |
组件吧 vuex是状态管理 |
如果请求的数据是多个组件共享的,为了方便只写一份,就写vuex里面,如果是组件独用的就写在当前组件里面。 |
写vuex的action中,统一管理。 |
写在actions中,为了复用 |
个人认为如果请求回来的数据如果是多次在多个组件中共用的话,就写在vuex这个统一数据管理仓库中,并放在vuex的actions属性中定义请求函数(请求是异步的,不能放在mutations中),然后在用到请求数据的地方通过$this.dispatch(‘actions名称’)去触发执行请求。 但是如果请求只在该组件用到的话就没必要放在vuex中了,直接在该组件的methods中请求即可。 |
莫名其妙的问题,强行凑数的? 数据需要在vuex中维护就在actions里面请求,否则在组件内请求。具体情况具体分析。 |
根据业务场景划分,如果该请求数据的方法是多个视图共享的话,则写在action中,如果是当前视图所用,则写在组件的methods中 |
[vue] 请求数据是写在组件的methods中还是在vuex的action中?
The text was updated successfully, but these errors were encountered: