Vuex Module-状态仓库分割的使用介绍

广告:宝塔Linux面板高效运维的服务器管理软件 点击【 https://www.bt.cn/p/uNLv1L 】立即购买

Vuex Module-状态仓库分割的使用介绍

vuex构成

vuex主要包含以下五个部分:

State // 存储变量、数据Getter // 类似计算属性Mutation // 唯一修改state的方法Action // 异步调用MutationModule // 将store模块化vuex的modules使用创建目录

在此示例中,我创建了两个store文件,分别是 profile.jscustom.js,一个根文件index.js

custom.js

const customs = {    namespaced: true, // 创建命名空间    state: { // 存储变量        showAlert: false    },    mutations: { // 定义修改state方法        CHANGESHOW: (state, params) => {            state.showAlert = !state.showAlert        }    },    actions: { // 异步调用mutations        setShow: ({ commit }) => {            commit('CHANGESHOW')        }    },    getters: { // 将数据过滤输出        bodyShow: state => state.showAlert    }}export default customs
登录后复制

profile.js

const profile = {  namespaced: true,  state: {    name: 'common name',    age: 18,    bool: false  },  mutations: {    CHANGEMSG: (state, params) => {      state.name = params    },    CHANGEAGE: (state, params) => {      state.name = params    },    CHANGEBOOL: (state) => {      state.bool = !state.bool    }  },  actions: {    setName: ({ commit }) => {      commit('CHANGEMSG', 'Vuex common name')    },    setAge: ({ commit }) => {      commit('CHANGEAGE', 81)    },    setBool: ({ commit }) => {      commit('CHANGEBOOL')    }  },  getters: {    vuexName: state => state.name,    vuexAge: state => state.age,    vuexBool: state => state.bool  }}export default common
登录后复制

index.js

import Vue from 'vue'import Vuex from 'vuex'// 引入子storeimport profile from './modules/profile'import customs from './modules/customs'// Vue.use(Vuex)const store = new Vuex.Store({  modules: {    profile,    customs  }})export default store // 导出store,以便于后续使用
登录后复制

在需要使用的.vue文件里进行使用。方法如下

index.vue

<template><div>  name: <h5>{{vuexName}}</h5> <button @click='setName'>chenge name</button>      age: <h5>{{vuexAge}}</h5> <button @click='setAge'>chenge age</button>      bool: <h5>{{vuexBool}}</h5> <button @click='setBool'>chenge bool</button>      <br/>            <span @click='setShow' style='display:inline-block;width:200px;height:30px;border:1px solid #999;border-radius:5px;text-align:center;line-height:30px;cursor: pointer;'>click me ,change showAlert</span>      <em>{{bodyShow}}</em></div></template><script>import { mapActions, mapGetters } from 'vuex'export default {computed: {    ...mapGetters('profile', ['vuexName', 'vuexAge', 'vuexBool']),    ...mapGetters('customs', ['bodyShow'])  },methods: {    ...mapActions('customs', ['setShow']),    ...mapActions('profile', ['setName', 'setAge', 'setBool']),}</script><style></style>
登录后复制

app.js

import Vue from 'vue';import VueRouter from 'vue-router';// styleimport './../../sass/app.scss';// Componentsimport Main from './Main.vue';import routes from './routes';// storeimport store from './store';  // 将store挂载到VueVue.use(VueRouter);const router = new VueRouter({  routes,  saveScrollPosition: true,});new Vue({ router, store, ...Main }).$mount('#app');
登录后复制

初始效果图 ⬇️ 点击按钮之后效果图 ⬇️ 至此,modules使用流程演示完毕!【相关推荐:vue.js视频教程】

以上就是Vuex Module-状态仓库分割的使用介绍的详细内容,更多请关注9543建站博客其它相关文章!

广告:SSL证书一年128.66元起,点击购买~~~

9543建站博客
一个专注于网站开发、微信开发的技术类纯净博客。

作者头像
admin创始人

肥猫,知名SEO博客站长,14年SEO经验。

上一篇:uniapp如何添加请求拦截器
下一篇:Laravel 8.73 发布啦,看看都有哪些更新?

发表评论

关闭广告
关闭广告