ux.*_*eer 13 vuex vuejs3 vue-composition-api vuex4 vue-script-setup
我正在将组件从常规 Vue 3 Composition API 重构为脚本设置语法。初始点:
<script lang="ts">
import { defineComponent, computed } from 'vue';
import { mapGetters } from 'vuex';
export default defineComponent({
name: 'MyCoolBareComponent',
computed: {
...mapGetters('auth', ['isAdmin']),
},
});
</script>
Run Code Online (Sandbox Code Playgroud)
当前Vue v3 迁移文档,SFC Composition API Syntax Sugar (<脚本设置>),链接到此 RFC 页面:https://github.com/vuejs/rfcs/pull/182
使用计算反应性属性只有一个示例:
export const computedMsg = computed(() => props.msg + '!!!')
Run Code Online (Sandbox Code Playgroud)
由于当前没有可用的 Vuex 4 文档提到<scrip setup>,所以我仍然不清楚在使用此语法时应该如何使用?mapGetters或者使用 Vuex 4 解决这个问题的正确方法是什么?
Mar*_*der 15
tldr:向下滚动到最终结果
现在有更好的文档,简单的答案是:您不需要mapGetters,但可以自己实现。
https://next.vuex.vuejs.org/guide/composition-api.html#accessing-state-and-getters
<script setup>
import { computed } from 'vue'
import { useStore } from 'vuex'
const store = useStore()
const count = computed(() => store.getters.count)
</script>
Run Code Online (Sandbox Code Playgroud)
如果你有很多 getter 想要变成“计算属性”,你可以使用像这样“直观”的东西:
const { countIsOdd, countIsEven } = Object.fromEntries(Object.keys(store.getters).map(getter => [getter, computed(() => store.getters[getter])]))
Run Code Online (Sandbox Code Playgroud)
将其放入一个函数中,它看起来甚至很不错。
const mapGetters = (getters) => {
return Object.fromEntries(Object.keys(getters).map(getter => [getter, computed(() => getters[getter])]))
}
const { countIsOdd, countIsEven } = mapGetters(store.getters)
Run Code Online (Sandbox Code Playgroud)
将该函数放入文件中并将其导出为模块......
// lib.js
import { computed } from 'vue'
import { useStore } from 'vuex'
const mapGetters = () => {
const store = useStore()
return Object.fromEntries(Object.keys(store.getters).map(getter => [getter, computed(() => store.getters[getter])]))
}
export { mapGetters }
Run Code Online (Sandbox Code Playgroud)
...您可以轻松地在所有组件中使用它。
// components/MyComponent.vue
<script setup>
import { mapGetters } from '../lib'
const { countIsOdd, countIsEven } = mapGetters()
</script>
Run Code Online (Sandbox Code Playgroud)
这是我想出的最终 lib.js:
import { computed } from 'vue'
import { useStore } from 'vuex'
const mapState = () => {
const store = useStore()
return Object.fromEntries(
Object.keys(store.state).map(
key => [key, computed(() => store.state[key])]
)
)
}
const mapGetters = () => {
const store = useStore()
return Object.fromEntries(
Object.keys(store.getters).map(
getter => [getter, computed(() => store.getters[getter])]
)
)
}
const mapMutations = () => {
const store = useStore()
return Object.fromEntries(
Object.keys(store._mutations).map(
mutation => [mutation, value => store.commit(mutation, value)]
)
)
}
const mapActions = () => {
const store = useStore()
return Object.fromEntries(
Object.keys(store._actions).map(
action => [action, value => store.dispatch(action, value)]
)
)
}
export { mapState, mapGetters, mapMutations, mapActions }
Run Code Online (Sandbox Code Playgroud)
在组件中使用它看起来像这样:
<template>
Count: {{ count }}
Odd: {{ counterIsOdd }}
Even: {{ counterIsEven }}
<button @click="countUp">count up</button>
<button @click="countDown">count down</button>
<button @click="getRemoteCount('https://api.countapi.xyz')">
get remote count
</button>
</template>
<script setup>
import { mapState, mapGetters, mapMutations, mapActions } from '../lib'
// computed properties
const { count } = mapState()
const { countIsOdd, countIsEvent } = mapGetters()
// commit/dispatch functions
const { countUp, countDown } = mapMutations()
const { getRemoteCount } = mapActions()
</script>
Run Code Online (Sandbox Code Playgroud)
对此的任何反馈将不胜感激。
到目前为止,这个语法似乎有效。然而,我希望 Vuex 能够开发一种更干净的方法来公开模板的计算 getter。
如果您知道更好的方法,我们很乐意听到!
<script setup lang="ts">
import { mapGetters } from 'vuex';
export const name = 'MyCoolBareComponent';
export default {
computed: {
...mapGetters('user', ['profile', 'roles']),
},
};
</script>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22502 次 |
| 最近记录: |