vuex-class和vuex-module-decorators 使用异同
vuex-class 和 vuex-module-decorators 的区别
vuex-class 是辅助vuex 使用的,主要有几个类似注解的装饰器
@State(‘foo’)
@State(state => state.bar)
@Getter(‘foo’)
@Action(‘foo’)
@Mutation(‘foo’)
@someModule.Getter(‘foo’)
这些都是写在组件vue 界面上的。
vuex-module-decorators 主要作用于vuex 的store 的写法上,主要有几个类似注解的装饰器
@Module
@Mutation
@Action
@MutationAction
用法上,vuex-class 在vuex 上使用的时候,需要自己记录store 的访问的路径比如
const someModule = namespace(‘path/to/module’)
而 另外一个只需要引用 store 的module 就行了。
而且可以在visual studio code 中带有提示。这是在vue 组件界面上书写的。
const postsModule = getModule(Posts)
// access posts
const posts = postsModule.posts
// use getters
const commentCount = postsModule.totalComments
// commit mutation
postsModule.updatePosts(newPostsArray)
// dispatch action
await postsModule.fetchPosts()
注意:
https://championswimmer.in/vuex-module-decorators/pages/core/state.html 文档页面强调了如下图所示的内容,我就遇到了这样的问题,如果不初始化,将无法拿到内容,心酸。