我想問一些事情,我有一個定制商店,比如
const BlaBla = (Data) => {
const { subscribe, set, update } = writable(Data);
return {
subscribe,
update,
set,
setData: (NewData) => {
set(NewData)
},
getData: () => {
return <<<<<<< "Here lies the problem, how i can get the "newData"?."
}
}
}
我將展示這個場景,我為一個 Fivem 服務器創建一個腳本,我使用 svelte,我創建了一個商店,它獲得了一個具有一些屬性(如名稱、姓氏、車牌和 bla bla)的車輛,我創建了 setData(Vehicle) 并通過一組(車輛)然后在另一種方法中我只想“獲取”車牌,我所做的一個解決方案是在范圍內創建一個變數,而不是一組我做了這樣的更新
const VehicleStore = (Vehicle) => {
let Data = {} //Variable inside the scope
const { subscribe, set, update } = writable(Vehicle);
return {
subscribe,
update,
set,
setData: (NewData) => {
update((s) => {
s = NewData
Data = s
return s
})
},
getData: () => {
return Data.Plate
}
}
}
我不知道這是否是實際的解決方案,我想我錯過了一些東西
uj5u.com熱心網友回復:
Svelte 匯出了一個get函式,可以用來決議一次 store 的值(它是語法糖subscribe)。
所以首先你必須得到 store 的值,然后你可以訪問它的屬性:
import { get } from 'svelte/store';
// ...
const store = writable(Data);
const { subscribe, set, update } = store;
// ...
return get(store).Plate
請注意,訪問這樣的資料不會是被動的,因為沒有對存盤的持久訂閱。您通常不打算使用這樣的商店。
相反,您通常會通過以下方式使用自動訂閱$在組件的標記中使用商店:
$VehicleStore.Plate
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/487775.html
標籤:javascript 打字稿 店铺 苗条
