關(guān)于vue3 vuex4 store的響應(yīng)式取值問題解決
場景:
在頁面中點擊按鈕,數(shù)量增加,值是存在store中的,點擊事件,值沒變。
<script setup lang="ts">
import { useStore } from '@/vuex';
const store = useStore()
const onSubmit = () => {
store.dispatch("incrementAction", 1);
}
let count = store.state.count
</script>
<template>
<h1 @click="onSubmit">{{ count }}</h1>
</template>原因:store.state.count錯誤的取值方式,雖然可以取出,但是喪失了響應(yīng)式,也就是觸發(fā)increment事件時候,count的值不會變化
解決:
<script setup lang="ts">
import { useStore } from '@/vuex';
import {computed} from 'vue'
const store = useStore()
const onSubmit = () => {
store.dispatch("incrementAction", 1);
}
let num = computed(() => store.state.count)
</script>
<template>
<h1 @click="onSubmit">{{ count }}</h1>
<h1>{{$store.state.count}}</h1>
</template>或者,標簽中用$store.state.count也能取得響應(yīng)式的值。
到此這篇關(guān)于vue3 vuex4 store的響應(yīng)式取值的文章就介紹到這了,更多相關(guān)vue3 vuex4取值內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue拖拽組件 vuedraggable API options實現(xiàn)盒子之間相互拖拽排序
這篇文章主要介紹了vue拖拽組件 vuedraggable API options實現(xiàn)盒子之間相互拖拽排序克隆clone,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-07-07
Vue Element UI 中 el-table 樹形數(shù)據(jù) 
這篇文章主要介紹了Vue Element UI 中 el-table 樹形數(shù)據(jù) tree-props 多層級使用避坑指南,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-01-01

