视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
vuex模块化和命名空间的实例代码
2020-11-27 19:32:44 责编:小采
文档


这篇文章给大家介绍的内容是关于vuex模块化和命名空间的实例代码 ,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

因为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>
 <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 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

相关推荐:

vue组件是什么?Vue组件的的介绍

Vue子组件与父组件之间的通信(附代码)

下载本文
显示全文
专题