我正尝试在类星体应用程序(Vue 3/ TypeScript)中使用带有Pinia的TypeScript。
一切都很好。
但是,当使用类星体引导文件时,持久化状态将停止工作。刷新页面会擦除所有新值。
我不知道引导文件为什么会破坏持久化的状态插件,但我已经将罪魁祸首缩小到了一行.
这就是我如何在类星体中使用Pinia并添加插件:
src/store/index.ts
/* eslint-disable @typescript-eslint/no-unused-vars */
import { store } from 'quasar/wrappers';
import { createPinia, Pinia } from 'pinia';
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate';
declare module '@quasar/app' {
interface BootFileParams<TState> {
store: Pinia;
}
interface PreFetchOptions<TState> {
store: Pinia;
}
}
declare module '@vue/runtime-core' {
interface ComponentCustomProperties {
$store: import('pinia').Pinia;
}
}
export default store(function (_) {
const pinia = createPinia();
pinia.use(piniaPluginPersistedstate); // Pinia Plugin added here
return pinia;
});这就是我的皮尼亚店的样子:
src/store/user.ts
import { defineStore } from 'pinia';
export const useUserStore = defineStore('user', {
state: () => {
return {
user: {
firstName: 'Mary',
},
};
},
persist: true, // Note that we are using a persisted state here
actions: {
setFirstName(firstName: string) {
this.user.firstName = firstName;
console.log('Name set to Pinia store: ', this.user.firstName);
},
getFirstName() {
if (!this.user.firstName) {
console.log('No name found in store. Setting "John" to Pinia store.');
this.user.firstName = 'John';
return this.user.firstName;
} else {
console.log('Name fetched from Pinia store: ', this.user.firstName);
return this.user.firstName;
}
},
},
});下面是一个获取和设置firstName的前端页面示例:
src/pages/index.vue
<template>
<div>{{ firstName }}</div>
<q-form @submit="handleFirstNameSubmit">
<p>Change First Name</p>
<q-input v-model="firstNameInput" filled outline />
<q-btn label="Submit Name to Pinia Store" type="submit" />
</q-form>
<q-btn @click="handleFirstNameFetch" label="Fetch Name from Pinia Store" />
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { useUserStore } from 'src/store/user';
const userStore = useUserStore();
const firstName = ref<string>();
const firstNameInput = ref<string>();
const handleFirstNameSubmit = () => {
if (firstNameInput.value) {
userStore.setFirstName(firstNameInput.value);
}
};
const handleFirstNameFetch = () => {
firstName.value = userStore.getFirstName();
};
</script>到目前为止,一切都很好。
我可以将firstName设置为Pinia商店,刷新页面,新名称仍在Pinia中。
但是,当在引导文件中使用const userStore = useUserStore(store)时(如下面的示例所示),持久化状态将停止工作:
src/boot/auth.ts
import { boot } from 'quasar/wrappers';
import { useUserStore } from 'src/store/user';
export default boot(({ store }) => {
const userStore = useUserStore(store);
// Do some other authentication stuff, setting initial user store values etc, below here...
});知道怎么回事吗?以及如何修复它?
我认为这个插件比使用替代的LocalStorage持久化状态解决方案要干净得多,所以我很想让它与类星体一起工作。
发布于 2022-05-23 10:05:12
类星体应用程序的一个常见用例是在根Vue应用实例实例化之前运行代码。
如果应用程序没有实例化,那么pinia插件还没有安装。请参阅:https://github.com/vuejs/pinia/discussions/723#discussioncomment-2110660
发布于 2022-11-04 15:02:58
大家经过大量的研究,我找到了这个问题的答案,您必须将index.ts/js传递给const,如下所示:
这是为我在类星体工作。)
<script lang="ts" setup>
import store from '../stores/index';
import { useCounterStore } from '../stores/counter';
const counterStore = useCounterStore(store());
counterStore.increment();
console.log(counterStore.count);
</script>https://stackoverflow.com/questions/71393805
复制相似问题