2021-03-05 19:10:39 +08:00
|
|
|
|
# @fesjs/plugin-vuex
|
2022-05-16 14:48:25 +08:00
|
|
|
|
|
|
|
|
|
::: tip
|
|
|
|
|
vue3+ 官方推荐使用[pinia](./pinia),不在推荐使用 vuex。
|
|
|
|
|
:::
|
|
|
|
|
|
2021-03-05 19:10:39 +08:00
|
|
|
|
## 介绍
|
|
|
|
|
|
2022-05-16 14:48:25 +08:00
|
|
|
|
集成 vuex 插件
|
|
|
|
|
|
|
|
|
|
增强 vuex,导出所有的`mutations`、`actions`和`getter`的事件类型,编辑器提示
|
|
|
|
|
|
|
|
|
|
约定模式,module 和 plugin 定义放在 stores 目录下,文件名包含 plugin 被解析为插件,无需额外配置,定义即可用。
|
2021-03-05 19:10:39 +08:00
|
|
|
|
|
2022-04-11 20:02:07 +08:00
|
|
|
|
```
|
|
|
|
|
└── src
|
|
|
|
|
├── pages
|
|
|
|
|
│ └── index.vue
|
2022-05-16 14:48:25 +08:00
|
|
|
|
└── stores
|
2022-04-11 20:02:07 +08:00
|
|
|
|
│ └── foo
|
|
|
|
|
│ │ └── bar.js
|
|
|
|
|
│ ├── counter.js
|
2022-05-16 14:48:25 +08:00
|
|
|
|
│ ├── plugin-logger.js
|
2022-04-11 20:02:07 +08:00
|
|
|
|
│ ├── user.js
|
|
|
|
|
└── app.js
|
|
|
|
|
```
|
2022-05-16 14:48:25 +08:00
|
|
|
|
|
2021-03-05 19:10:39 +08:00
|
|
|
|
::: tip
|
2022-05-16 14:48:25 +08:00
|
|
|
|
为了防止`fesjs`与`vuex`的 export 冲突,fesjs 不提供导出 vuex 的任何 api。你可以直接使用 vuex 的 api
|
|
|
|
|
|
2022-04-11 20:02:07 +08:00
|
|
|
|
```js
|
|
|
|
|
import { useStore } from 'vuex';
|
|
|
|
|
```
|
2022-05-16 14:48:25 +08:00
|
|
|
|
|
2021-03-05 19:10:39 +08:00
|
|
|
|
:::
|
2022-05-16 14:48:25 +08:00
|
|
|
|
|
2021-03-05 19:10:39 +08:00
|
|
|
|
## 启用方式
|
2022-05-16 14:48:25 +08:00
|
|
|
|
|
2021-03-05 19:10:39 +08:00
|
|
|
|
在 `package.json` 中引入依赖:
|
2022-05-16 14:48:25 +08:00
|
|
|
|
|
2021-03-05 19:10:39 +08:00
|
|
|
|
```json
|
|
|
|
|
{
|
|
|
|
|
"dependencies": {
|
|
|
|
|
"@fesjs/fes": "^2.0.0",
|
|
|
|
|
"@fesjs/plugin-vuex": "^2.0.0"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 配置
|
2022-05-16 14:48:25 +08:00
|
|
|
|
|
2021-03-05 19:10:39 +08:00
|
|
|
|
在 `.fes.js` 中配置:
|
2022-05-16 14:48:25 +08:00
|
|
|
|
|
2021-03-05 19:10:39 +08:00
|
|
|
|
```js
|
|
|
|
|
export default {
|
|
|
|
|
vuex: {
|
2022-05-16 14:48:25 +08:00
|
|
|
|
strict: true, // 开启严格模式
|
|
|
|
|
},
|
|
|
|
|
};
|
2021-03-05 19:10:39 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 场景使用
|
2022-05-16 14:48:25 +08:00
|
|
|
|
|
|
|
|
|
先定义在 stores 下定义 user 模块,包含嵌套模块
|
2022-04-11 20:02:07 +08:00
|
|
|
|
|
|
|
|
|
stores/user.js
|
2022-05-16 14:48:25 +08:00
|
|
|
|
|
2021-03-05 19:10:39 +08:00
|
|
|
|
```js
|
2022-04-11 20:02:07 +08:00
|
|
|
|
export default {
|
|
|
|
|
namespaced: true,
|
|
|
|
|
state: () => ({
|
|
|
|
|
name: 'aring',
|
2022-05-16 14:48:25 +08:00
|
|
|
|
age: 20,
|
2022-04-11 20:02:07 +08:00
|
|
|
|
}),
|
|
|
|
|
actions: {
|
|
|
|
|
login() {
|
|
|
|
|
return new Promise((reslove) => {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
console.log('login');
|
|
|
|
|
reslove('OK');
|
|
|
|
|
}, 1000);
|
|
|
|
|
});
|
2022-05-16 14:48:25 +08:00
|
|
|
|
},
|
2022-04-11 20:02:07 +08:00
|
|
|
|
},
|
|
|
|
|
modules: {
|
|
|
|
|
address: {
|
|
|
|
|
state: () => ({
|
|
|
|
|
province: '广东省',
|
|
|
|
|
city: '深圳市',
|
2022-05-16 14:48:25 +08:00
|
|
|
|
zone: '南山区',
|
2022-04-11 20:02:07 +08:00
|
|
|
|
}),
|
|
|
|
|
getters: {
|
|
|
|
|
address(state) {
|
|
|
|
|
return state.province + state.city + state.zone;
|
2022-05-16 14:48:25 +08:00
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2022-04-11 20:02:07 +08:00
|
|
|
|
};
|
2021-03-05 19:10:39 +08:00
|
|
|
|
```
|
2022-05-16 14:48:25 +08:00
|
|
|
|
|
2022-04-11 20:02:07 +08:00
|
|
|
|
stores/foo/bar.js
|
2022-05-16 14:48:25 +08:00
|
|
|
|
|
2021-03-05 19:10:39 +08:00
|
|
|
|
```js
|
2022-04-11 20:02:07 +08:00
|
|
|
|
export default {
|
|
|
|
|
namespaced: true,
|
|
|
|
|
state: () => ({
|
2022-05-16 14:48:25 +08:00
|
|
|
|
count: 0,
|
2022-04-11 20:02:07 +08:00
|
|
|
|
}),
|
|
|
|
|
mutations: {
|
|
|
|
|
increment(state) {
|
|
|
|
|
state.count++;
|
2022-05-16 14:48:25 +08:00
|
|
|
|
},
|
2022-04-11 20:02:07 +08:00
|
|
|
|
},
|
|
|
|
|
getters: {
|
|
|
|
|
doubleCount(state) {
|
|
|
|
|
return state.count * 2;
|
2022-05-16 14:48:25 +08:00
|
|
|
|
},
|
2022-04-11 20:02:07 +08:00
|
|
|
|
},
|
|
|
|
|
actions: {
|
|
|
|
|
asyncIncrement({ commit }) {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
commit('increment');
|
|
|
|
|
}, 2000);
|
2022-05-16 14:48:25 +08:00
|
|
|
|
},
|
|
|
|
|
},
|
2022-04-11 20:02:07 +08:00
|
|
|
|
};
|
|
|
|
|
```
|
2022-05-16 14:48:25 +08:00
|
|
|
|
|
2022-04-11 20:02:07 +08:00
|
|
|
|
::: tip
|
|
|
|
|
导出的`mutations`、`actions`和`getter`的事件类型,将会按文件命名;
|
|
|
|
|
|
2022-05-16 14:48:25 +08:00
|
|
|
|
如`ACTION_TYPES.user.login`指向 user 模块中 actions 的 login 方法
|
2022-04-11 20:02:07 +08:00
|
|
|
|
|
2022-05-16 14:48:25 +08:00
|
|
|
|
如`GETTER_TYPES.user.address`指向 user 模块中嵌套的 address getter
|
2022-04-11 20:02:07 +08:00
|
|
|
|
|
2022-05-16 14:48:25 +08:00
|
|
|
|
如`MUTATION_TYPES.fooBar.increment`指向 foo/bar 模块中 mutations 的 increment 方法
|
2022-04-11 20:02:07 +08:00
|
|
|
|
:::
|
|
|
|
|
|
2022-05-16 14:48:25 +08:00
|
|
|
|
在 vue 文件中使用 store
|
|
|
|
|
|
2022-04-11 20:02:07 +08:00
|
|
|
|
```vue
|
|
|
|
|
<template>
|
|
|
|
|
<div>
|
|
|
|
|
<h4>Vuex</h4>
|
|
|
|
|
<div><button :disabled="disabled" @click="login">async login</button></div>
|
2022-05-16 14:48:25 +08:00
|
|
|
|
<div>
|
|
|
|
|
<button @click="fooBarIncrement">foo/bar:{{ fooBarDoubleCount }}</button>
|
|
|
|
|
</div>
|
|
|
|
|
<div>{{ address }}</div>
|
2022-04-11 20:02:07 +08:00
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
<config>
|
|
|
|
|
{
|
|
|
|
|
"name": "store",
|
|
|
|
|
"title": "vuex测试"
|
|
|
|
|
}
|
|
|
|
|
</config>
|
|
|
|
|
<script>
|
|
|
|
|
import { computed, ref } from 'vue';
|
2021-03-05 19:10:39 +08:00
|
|
|
|
import { useStore } from 'vuex';
|
|
|
|
|
import { MUTATION_TYPES, GETTER_TYPES, ACTION_TYPES } from '@fesjs/fes';
|
2022-04-11 20:02:07 +08:00
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
setup() {
|
|
|
|
|
const store = useStore();
|
|
|
|
|
console.log('store==>', store);
|
|
|
|
|
const disabled = ref(false);
|
|
|
|
|
// 可以利用导出的事件类型,不再通过字符传入(store.getters['user/address'])
|
|
|
|
|
return {
|
|
|
|
|
address: computed(() => store.getters[GETTER_TYPES.user.address]),
|
|
|
|
|
disabled,
|
|
|
|
|
login: () => {
|
|
|
|
|
disabled.value = true;
|
|
|
|
|
store.dispatch(ACTION_TYPES.user.login).then((res) => {
|
|
|
|
|
window.alert(res);
|
|
|
|
|
disabled.value = false;
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
fooBarIncrement: () => store.commit(MUTATION_TYPES.fooBar.increment), // foo/bar目录会解析成驼峰fooBar
|
2022-05-16 14:48:25 +08:00
|
|
|
|
fooBarDoubleCount: computed(() => store.getters[GETTER_TYPES.fooBar.doubleCount]),
|
2022-04-11 20:02:07 +08:00
|
|
|
|
};
|
2022-05-16 14:48:25 +08:00
|
|
|
|
},
|
2022-04-11 20:02:07 +08:00
|
|
|
|
};
|
|
|
|
|
</script>
|
2021-03-05 19:10:39 +08:00
|
|
|
|
```
|
2022-04-11 20:02:07 +08:00
|
|
|
|
|
|
|
|
|
::: tip
|
2022-05-16 14:48:25 +08:00
|
|
|
|
由于该插件注册在 onAppCreated 中,如果在 onAppCreated 及之前使用 useStore 时,获取不到 vuex 实例
|
|
|
|
|
|
|
|
|
|
`fesjs`导出了 vuex 实例`store`,如在 app.js 文件中
|
2022-04-11 20:02:07 +08:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
import { store, GETTER_TYPES } from '@fesjs/fes';
|
2022-05-16 14:48:25 +08:00
|
|
|
|
console.log(store.getters[GETTER_TYPES.user.address]);
|
2022-04-11 20:02:07 +08:00
|
|
|
|
```
|
2022-05-16 14:48:25 +08:00
|
|
|
|
|
2022-04-11 20:02:07 +08:00
|
|
|
|
:::
|
|
|
|
|
|
2022-05-16 14:48:25 +08:00
|
|
|
|
## vuex 插件
|
|
|
|
|
|
|
|
|
|
stores 文件夹下的文件名包含 plugin 被解析为插件,vuex 插件写法参考[官方文档](https://next.vuex.vuejs.org/guide/plugins.html)
|
|
|
|
|
|
2021-03-05 19:10:39 +08:00
|
|
|
|
## API
|
2022-04-11 20:02:07 +08:00
|
|
|
|
|
|
|
|
|
### store
|
2022-05-16 14:48:25 +08:00
|
|
|
|
|
|
|
|
|
- 类型 `Object`
|
|
|
|
|
- vuex 实例
|
|
|
|
|
|
2021-03-05 19:10:39 +08:00
|
|
|
|
### MUTATION_TYPES
|
2022-05-16 14:48:25 +08:00
|
|
|
|
|
|
|
|
|
- 类型 `Object`
|
|
|
|
|
- mutation 的所有事件类型
|
|
|
|
|
|
2021-03-05 19:10:39 +08:00
|
|
|
|
### GETTER_TYPES
|
2022-05-16 14:48:25 +08:00
|
|
|
|
|
|
|
|
|
- 类型 `Object`
|
|
|
|
|
- getter 的所有方法名
|
|
|
|
|
|
2021-03-05 19:10:39 +08:00
|
|
|
|
### ACTION_TYPES
|
2022-05-16 14:48:25 +08:00
|
|
|
|
|
|
|
|
|
- 类型 `Object`
|
|
|
|
|
- action 的所有事件类型
|