因为Vuex Store是全局注册的,不利于较大的项目,引入模块分离业务状态和方法,引入命名空间解决不同模块内(getters,mutaions,actions)名称冲突的问题

------------------------------------------------------------------

首先建立一个模块 ./store/modules/sample.js

import SampleAPI from '@/api/sample-api-proxy.js'
import { _AjaxUrl } from '@/store/constants' const state = {
all: []
}
const mutations = {
resolve (state, payload) {
for (let item of payload) {
state.all.push(item)
}
}
}
const getters = {
allstr (state) {
return state.join(',')
}
}
const actions = {
async init ({commit,state}, pid) {
await SampleAPI.get(_AjaxUrl + '/api/game/all', params: {pid}).then((res) => {
state.all = res.all
commit('resolve', res.data)
})
}
} export default {
namespaced: true,
state, mutations, getters, actions
}

./store/index.js 注入模块

import Vuex from 'vuex'
import sample_module from './modules/sample'
import other_module from './modules/other' export default new Vuex.Store({
//全局Store对象
mutations,
actions,
state, //模块
modules: {
sample: sample_module,
other: other_module
}
})

启动程序(main.js)中注册 store 到根组件

import Vue from 'vue'
import Vuex from 'vuex' Vue.use(Vuex)
new Vue({
el: '#app',
data() {
return { rootParam: 'test' }
},
store,
router,
template: '<Home/>',
components: { Home }
})

页面组件(./components/sample.vue)中声明并调用

<template>
<div id="container">
<ul class="sample-ul">
<li v-for="(item, index) in all" :key="index">
<span>{{item}}</span>
<button @click="removeItem(index)">删除</button>
</li>
</ul>
<div>{{all2str}}</div>
</div>
</template> <style lang="stylus" rel="stylesheet/stylus" scoped>
@import '~style/common.styl'
nospace()
margin 0
padding 0
height(h)
height unit(h, 'px')
line-height unit(h, 'px') .sample-ul
list-style-type none
@nospace
li
display block
height(20)
&:hover
background-color #fcc
</style> <script type="text/ecmascript-6">
import { createNamespacedHelpers, mapState } from 'vuex'
const { mapActions, mapGetters, mapMutations } = createNamespacedHelpers('sample')
const { mapActions: mapOtherActions, mapGetters: mapOtherGetters } = createNamespacedHelpers('other') export default{
data() {
return { }
},
computed: {
...mapState({
all : state => state.sample.all
}),
...mapGetters(['all2str']),
...mapOtherGetters(['test'])
},
methods: {
...mapActions(['loadDataAsync']),
...mapMutations(['removeItem']),
...mapOtherMutations(['testing'])
},
created() {
const pid = this.$route.query.pid
this.loadDataAsync(keep, pid)
}
}
</script>

推荐使用对象展开运算符将 mapMutations,mapGetters,mapActions,mapState 混入到页面组件,页面仅用于交互体验,不要参杂过多的业务逻辑和状态
通过 createNamespacedHelpers 映射到命名空间

项目结构:

├── index.html
├── main.js
├── api
│ ├── sample-api-proxy.js
│ └── ...
├── components
│ ├── sample.vue
│ └── ...
└── store
├── index.js
├── actions.js
├── mutations.js
├── state.js
└── modules
├── sample.js
└── other.js

最新文章

  1. .net动态类型在处理json数据方面的应用
  2. vijos1037搭建双塔(一维背包问题)
  3. 安卓中級教程(1):@InjectView
  4. hihoCoder太阁最新面经算法竞赛18
  5. elasticsearch【更新】操作
  6. java多线程:并发包中ConcurrentHashMap和jdk的HashMap的对比
  7. Js数组去重复取唯一值
  8. Java将一段逗号分割的字符串转换成一个数组
  9. js 获取 sktime时间
  10. python日期时间处理
  11. debian下安装repo
  12. JavaScript 特效三大系列总结
  13. Spring Security 入门(1-8)缓存EhCache
  14. Linux 句柄是什么 ?
  15. Python @property 详解
  16. Python之print()函数
  17. Spring Boot 静态页面
  18. 结对项目junit测试用例
  19. PC端和移动端在前端开发上的一些区别,前端里移动端到底比pc端多哪些知识
  20. Android-多线程AsyncTask

热门文章

  1. 怎么把微信里的文件发到QQ?
  2. koa 学习1
  3. java webservice生成客户端代码并调用
  4. 初学html,任务1:一个简单html页面,要求:内容页面装一篇文章 用html来分段
  5. android studio 模拟器不能使用的解决方案
  6. 处理 NCBI taxonomy tree
  7. Sublime Text 3安装emmet(ZenCoding)
  8. 《数学之美》--第一章:文字和语言 vs 数字和信息
  9. hashtable and hashmap
  10. python语法之正则