我有一个 react 值,我想将其存储在 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;
但是当我这样做时, react 性就消失了。您如何将值存储在商店中,以便每次更新 data
时更新 myData
?
最佳答案
您应该这样定义您的商店:
import {defineStore} from 'pinia'
export const useMyDataStore = defineStore('myStore', {
state: () => ({
myData: {}
}),
actions: {
update(value){
Object.assign(this.myData, value);
}
}
})
然后用like
const data = ref({});
async function loadData() {
fetch("...")
.then((res) => res.json())
.then((json) => (data.value = json));
}
loadData().then();
const myDataStore = useMyDataStore();
watch(data, (newVal) => {
myDataStore.update(newVal);
}
关于vue.js - 在 pinia 中存储 react (ref) 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72085856/
相关文章:
node.js - 如何在工作区中使用 npm-shrinkwrap?
hibernate - 我在 grails 中遇到完整性违规异常。如何级联删除到grails中的相关
azure - 我正在将 AddMicrosoftIdentityWebApi .net core
reactjs - 如何让 Next Image 组件与 Storybook 一起工作
javascript - NextJS - 在 getStaticProps 中获取动态变量
visual-studio - 如何从VS2022状态栏中删除 "Add to Source Con