将PINIA中的反应性(参考)值存储
我有一个反应价值,我想将其存储在Pinia商店中。
const data = ref({});
async function loadData() {
fetch("...")
.then((res) => res.json())
.then((json) => (data.value = json));
}
loadData();
const myDataStore = useMyDataStore();
const { myData } = storeToRefs(myDataStore);
// Here I want to store data in the store, reactively
myData.value = data.value;
但是当我这样做时,反应性就会丢失。您如何将值存储在商店中,以便每次更新myData
都会更新。
I have a reactive value, which I want to store in a pinia store.
const data = ref({});
async function loadData() {
fetch("...")
.then((res) => res.json())
.then((json) => (data.value = json));
}
loadData();
const myDataStore = useMyDataStore();
const { myData } = storeToRefs(myDataStore);
// Here I want to store data in the store, reactively
myData.value = data.value;
But when I do this, the reactivity is lost. How do you store the value in the store, so that myData
is updated every time data
is updated?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
Watch()
查找REF数据对象的更改。在更改上呼叫mydatastore.update(newdata)
:Pinia中的状态应更新
Use
watch()
to look for changes on your ref data object. On change call themyDataStore.update(newData)
:The state in pinia should be updated
您应该这样定义商店:
然后使用
You should define your store like this:
Then use like
Pinia状态已经反应:
Nechoj的构图商店很整洁,这是我的最爱。
然后更新状态:
在模板中:
触发MyData突变的回调:
Pinia states are already reactive :
the composition store from Nechoj is neat, its my favorite.
then to update state:
in template:
to trigger callback on myData mutation: