To support vuex-composition map* helper functions English Document
yarn add vuex-composition-maphooks
// *.vue
import { useState, useGetters, useMutations, useActions } from 'vuex-composition-maphooks';
...
-
namespace
参数非必填,如果modules
设置namespaced: true
时必填 -
states
可选类型:数组 | 对象 (支持自定义states方法名) -
states 使用数组的用法
<script setup lang="ts">
import { ref } from 'vue';
import { useState } from 'vuex-composition-maphooks';
const { userinfo } = useState('A', ['userinfo']);
const user = ref(userinfo());
</script>
- states 使用对象的用法
...
const { d } = useState('A', { userinfo: 'd' });
const user = ref(d());
-
namespace
参数非必填,如果modules
设置namespaced: true
时必填 -
getters
可选类型:数组 | 对象 (支持自定义getters方法名) -
getters 使用数组的用法
<script setup lang="ts">
import { ref } from 'vue';
import { useGetters } from 'vuex-composition-maphooks';
const { unDoList, doList } = useGetters('A', ['unDoList', 'doList']);
// or
const { unDoList, doList } = useGetters(['A/unDoList', 'A/doList']);
const a = ref(unDoList());
const b = ref(doList());
</script>
- getters 使用对象的用法
...
const { d, e } = useGetters('A',{ unDoList: 'd', doList: 'e'});
// or
const { d, e } = useGetters({ 'A/unDoList': 'd', 'A/doList': 'e'});
const a = ref(d());
const b = ref(e());
...
-
namespace
参数非必填,如果modules
设置namespaced: true
时必填 -
mutations
可选类型:数组 | 对象 (支持自定义mutations方法名) -
mutations 使用数组的用法
<script setup lang="ts">
import { ref } from 'vue';
import { useMutations } from 'vuex-composition-maphooks';
const { INCREMENT } = useMutations('A', ['INCREMENT']);
// or
const { INCREMENT } = useMutations(['A/INCREMENT']);
</script>
- mutations 使用对象的用法
...
const { d } = useMutations('A', { 'INCREMENT': 'd'});
// or
const { d } = useMutations({ 'A/INCREMENT': 'd'});
...
-
namespace
参数非必填,如果modules
设置namespaced: true
时必填 -
actions
可选类型:数组 | 对象 (支持自定义actions方法名) -
actions 使用数组的用法
<script setup lang="ts">
import { useActions } from 'vuex-composition-maphooks';
// 场景一: A、B模块都不设置命明空间时
const { go, back } = useActions(['go','back']);
// 场景二:A模块设置命明空间,B模块不设置命明空间
const { go, back } = useActions(['A/go', 'back']);
// or
const { go } = useActions('A', ['go']);
const { back } = useActions(['back']);
// 场景三:A、B模块都设置命明空间
const { go, back } = useActions(['A/go', 'B/back']);
// or
const { go } = useActions('A', ['go']);
const { back } = useActions('B', ['back']);
</script>
- actions 使用对象的用法
...
// 场景一: A、B模块不设置命明空间时
const { d, e } = useActions({ go: 'd', back: 'e' });
// 场景二:A模块设置命明空间,B模块不设置命明空间
const { d, e } = useActions({ 'A/go': 'd', back: 'e' });
// or
const { d } = useActions({ 'A/go': 'd' });
const { e } = useActions({ back: 'd' });
// 场景三:A、B模块都设置命明空间
const { d, e } = useActions({ 'A/go': 'd', 'B/back': 'e' });
// or
const { d } = useActions({ 'A/go': 'd' });
const { e } = useActions({ 'B/back': 'e' });
...
namespace
非必填,但是设置namespaced: true
必填,并且强烈推荐设置namespaced
!!!useState
第二个参数的key
不能是拼接modules name
的字符串,必须是指定的state
useGetters
、useMutations
、useActions
第二个参数的key
可以拼接是modules name
的字符串,如上示例