vue3 中 pinia 的 state 修改模版绑定的 state 数据没更新?
html
<div v-for="(layer, index) in layerList" :key="layer.id" class="layer-item"> <div>{{ layer.name }}</div> </div>
JavaScript
解决方案 1 :
给 state 增加 computed
<script setup>
import { useLayerStore } from "@/stores/";
const layer = useLayerStore(); const layerList = computed(() => layer.layerList);
</script>
解决方案 2:
给 useStore 使用 storeToRefs
<script setup> import { useLayerStore } from "@/stores/"; import { storeToRefs } from "pinia"; const layer = useLayerStore(); const { layerList } = storeToRefs(layer); </script>