Appearance
Pinia
组件间共享数据对象。
用法
从服务端获取数据,然后可以在组件中使用
ts
export const useMarksStore = defineStore('marks', () => {
const list: Ref<MarkResource[]> = ref([]);
const load = async function () {
list.value = await new apiMarkList();
}
const names: Ref<string[]> = computed(() => {
return list.value.map(e => e.name);
})
return {load, names};
})
ts
const marks = useMarksStore();
const {load: marksLoad} = marks;
const {names: marksNames} = storeToRefs(marks);
await marksLoad();
console.dir(marksNames);