vue3+pinia的项目,我在store里建了一个test.js文件,代码如下:
import {defineStore} from "pinia";
import indexStore from "@/stores/index";
const testStore=defineStore("testStore",{
state:()=>{
return{
name:"小猪课堂",
age:25,
sex:'男'
}
}
})
export default testStore
然后分别在两个页面调用:
A页面:
import {storeToRefs} from 'pinia'
import testStore from '@/stores/test'
const testStorex=testStore()
const {name,age,sex}=storeToRefs(testStorex)
然后我把{{name}}打印到页面上.
B页面和A页面一样。
这样两个页面都显示“小猪课堂”。
然后我在A页面加一个按钮,添加点击事件:
const setX=()=>{
testStorex.name="intrwins"
}
我把name的值改成了"intwins"那我打开B页头不也应该显示“intrwins"吗?为什么却还显示着”小猪课堂“?????
仔细看文档,这样是不行的。你需要在 store 里定义一个 action,从里面修改 name。
或者你用
store.$patch({name: 'new name'})
可能也可以,这个我没试过。