目录结构
/src
/pages
/index
index.vue
/stores
index.ts
监听 index.vue 中数据变化
<script lang="ts" setup>
import { reactive, watch } from 'vue';
const data = reactive({
loading: false,
type: 0
})
watch(
() => [data.loading, data.type],
() => {
console.log('loading change: ', data.loading);
console.log('type change: ', data.type);
},
onTrigger(e) {
console.log('trigger:', e)
}
)
</script>
监听store 中state 值的变化
import { defineStore } from 'pinia';
const IndexStore = defineStore ({
id: 'index',
state: () => {
return {
status: 0,
}
}
})
export { IndexStore }
<script lang="ts" setup>
import { IndexStore } from '../../stores/index';
const indexStore = IndexStore()
indexStore.$subscribe((_mutation, state) => {
console.log(state.status)
})
</script>