首页 / VUE / vuex模块化和命名空间的实例代码
vuex模块化和命名空间的实例代码
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了vuex模块化和命名空间的实例代码,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3004字,纯文字阅读大概需要5分钟。
内容图文
![vuex模块化和命名空间的实例代码](/upload/InfoBanner/zyjiaocheng/286/c51572fa260d4a469f961c1859d51877.jpg)
因为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子组件与父组件之间的通信(附代码)
以上就是vuex模块化和命名空间的实例代码的详细内容,更多请关注Gxl网其它相关文章!
内容总结
以上是互联网集市为您收集整理的vuex模块化和命名空间的实例代码全部内容,希望文章能够帮你解决vuex模块化和命名空间的实例代码所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。