将PINIA中的反应性(参考)值存储

发布于 2025-01-26 01:13:57 字数 487 浏览 1 评论 0原文

我有一个反应价值,我想将其存储在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千里故人稀 2025-02-02 01:13:58

使用Watch()查找REF数据对象的更改。在更改上呼叫mydatastore.update(newdata)

watch(data, (newData) => {
   myDataStore.update(newData)
})

Pinia中的状态应更新

Use watch() to look for changes on your ref data object. On change call the myDataStore.update(newData):

watch(data, (newData) => {
   myDataStore.update(newData)
})

The state in pinia should be updated

触ぅ动初心 2025-02-02 01:13:57

您应该这样定义商店:

import {defineStore} from 'pinia'

export const useMyDataStore = defineStore('myStore', {
    state: () => ({
        myData: {}
    }),
    actions: {
        update(value){
            Object.assign(this.myData, value);
        }
    }
})

然后使用

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);
}

You should define your store like this:

import {defineStore} from 'pinia'

export const useMyDataStore = defineStore('myStore', {
    state: () => ({
        myData: {}
    }),
    actions: {
        update(value){
            Object.assign(this.myData, value);
        }
    }
})

Then use 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);
}
不气馁 2025-02-02 01:13:57

Pinia状态已经反应:

Nechoj的构图商店很整洁,这是我的最爱。

import {defineStore} from 'pinia'

export const useMyDataStore = defineStore('myStore', {
    state: () => ({
        myData: {}
    }),
    getters: {
        myReactiveData: ()=>{
            return this.myData
        }
    }
})

然后更新状态:

const data = ref({});
async function loadData() {
    return fetch("...")
        .then((res) => res.json())
}
loadData()
    .then((json) => {
        store.$state = { myData: json}
        // or myStore.$patch({myData:json}))
    }

在模板中:

<!-- always reactive -->
<p>{{myStore.myReactiveData}}</p>

触发MyData突变的回调:

function callbackFnA(mutation, state) {
    console.log(mutation)
    console.log(state)
}
myStore.$subscribe(callbackFnA)
// or with options: myStore.$subscribe(callbackFnA, options?)

Pinia states are already reactive :

the composition store from Nechoj is neat, its my favorite.

import {defineStore} from 'pinia'

export const useMyDataStore = defineStore('myStore', {
    state: () => ({
        myData: {}
    }),
    getters: {
        myReactiveData: ()=>{
            return this.myData
        }
    }
})

then to update state:

const data = ref({});
async function loadData() {
    return fetch("...")
        .then((res) => res.json())
}
loadData()
    .then((json) => {
        store.$state = { myData: json}
        // or myStore.$patch({myData:json}))
    }

in template:

<!-- always reactive -->
<p>{{myStore.myReactiveData}}</p>

to trigger callback on myData mutation:

function callbackFnA(mutation, state) {
    console.log(mutation)
    console.log(state)
}
myStore.$subscribe(callbackFnA)
// or with options: myStore.$subscribe(callbackFnA, options?)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文