"use strict";(self.webpackChunkfes_js=self.webpackChunkfes_js||[]).push([[132],{8462:(n,s,a)=>{a.r(s),a.d(s,{data:()=>p});const p={key:"v-5f4c684e",path:"/reference/plugin/plugins/vuex.html",title:"@fesjs/plugin-vuex",lang:"zh-CN",frontmatter:{},excerpt:"",headers:[{level:2,title:"介绍",slug:"介绍",children:[]},{level:2,title:"启用方式",slug:"启用方式",children:[]},{level:2,title:"配置",slug:"配置",children:[]},{level:2,title:"场景使用",slug:"场景使用",children:[]},{level:2,title:"vuex插件",slug:"vuex插件",children:[]},{level:2,title:"API",slug:"api",children:[{level:3,title:"store",slug:"store",children:[]},{level:3,title:"MUTATION_TYPES",slug:"mutation-types",children:[]},{level:3,title:"GETTER_TYPES",slug:"getter-types",children:[]},{level:3,title:"ACTION_TYPES",slug:"action-types",children:[]}]}],filePathRelative:"reference/plugin/plugins/vuex.md",git:{updatedTime:1653450562e3,contributors:[{name:"wanchun",email:"445436867@qq.com",commits:1}]}}},4565:(n,s,a)=>{a.r(s),a.d(s,{default:()=>r});var p=a(6252);const t=(0,p.uE)('
集成vuex插件
增强vuex,导出所有的mutations
、actions
和getter
的事件类型,编辑器提示
约定模式,module和plugin定义放在stores目录下,文件名包含plugin被解析为插件,无需额外配置,定义即可用。
└── src\n ├── pages\n │ └── index.vue\n └── stores \n │ └── foo\n │ │ └── bar.js\n │ ├── counter.js\n │ ├── plugin-logger.js \n │ ├── user.js\n └── app.js\n
提示
为了防止fesjs
与vuex
的export冲突,fesjs不提供导出vuex的任何api。你可以直接使用vuex的api
import { useStore } from 'vuex';\n
在 package.json
中引入依赖:
{\n "dependencies": {\n "@fesjs/fes": "^2.0.0",\n "@fesjs/plugin-vuex": "^2.0.0"\n }\n}\n
在 .fes.js
中配置:
export default {\n vuex: {\n strict: true // 开启严格模式\n }\n}\n
先定义在stores下定义user模块,包含嵌套模块
stores/user.js
export default {\n namespaced: true,\n state: () => ({\n name: 'aring',\n age: 20\n }),\n actions: {\n login() {\n return new Promise((reslove) => {\n setTimeout(() => {\n console.log('login');\n reslove('OK');\n }, 1000);\n });\n }\n },\n modules: {\n address: {\n state: () => ({\n province: '广东省',\n city: '深圳市',\n zone: '南山区'\n }),\n getters: {\n address(state) {\n return state.province + state.city + state.zone;\n }\n }\n }\n }\n};\n
stores/foo/bar.js
export default {\n namespaced: true,\n state: () => ({\n count: 0\n }),\n mutations: {\n increment(state) {\n state.count++;\n }\n },\n getters: {\n doubleCount(state) {\n return state.count * 2;\n }\n },\n actions: {\n asyncIncrement({ commit }) {\n setTimeout(() => {\n commit('increment');\n }, 2000);\n }\n }\n};\n
提示
导出的mutations
、actions
和getter
的事件类型,将会按文件命名;
如ACTION_TYPES.user.login
指向user模块中actions的login方法
如GETTER_TYPES.user.address
指向user模块中嵌套的address getter
如MUTATION_TYPES.fooBar.increment
指向foo/bar模块中mutations的increment方法
在vue文件中使用store
<template>\n <div>\n <h4>Vuex</h4>\n <div><button :disabled="disabled" @click="login">async login</button></div>\n <div><button @click="fooBarIncrement">foo/bar:{{fooBarDoubleCount}}</button></div>\n <div>{{address}}</div>\n </div>\n</template>\n<config>\n{\n "name": "store",\n "title": "vuex测试"\n}\n</config>\n<script>\nimport { computed, ref } from 'vue';\nimport { useStore } from 'vuex';\nimport { MUTATION_TYPES, GETTER_TYPES, ACTION_TYPES } from '@fesjs/fes';\n\nexport default {\n setup() {\n const store = useStore();\n console.log('store==>', store);\n const disabled = ref(false);\n // 可以利用导出的事件类型,不再通过字符传入(store.getters['user/address'])\n return {\n address: computed(() => store.getters[GETTER_TYPES.user.address]),\n disabled,\n login: () => {\n disabled.value = true;\n store.dispatch(ACTION_TYPES.user.login).then((res) => {\n window.alert(res);\n disabled.value = false;\n });\n },\n fooBarIncrement: () => store.commit(MUTATION_TYPES.fooBar.increment), // foo/bar目录会解析成驼峰fooBar\n fooBarDoubleCount: computed(() => store.getters[GETTER_TYPES.fooBar.doubleCount])\n };\n }\n};\n</script>\n\n
提示
由于该插件注册在onAppCreated中,如果在onAppCreated及之前使用useStore时,获取不到vuex实例
fesjs
导出了vuex实例store
,如在app.js文件中
import { store, GETTER_TYPES } from '@fesjs/fes';\nconsole.log(store.getters[GETTER_TYPES.user.address])\n
Object
Object
Object
Object