我在类星体的引导文件中将axios添加到了我的Pinia商店
export default boot(async ({ app, store, ssrContext, router }) => {
const api = axios.create({
baseURL: import.meta.env.VITE_APP_API_BASE_URL,
paramsSerializer: serializeParameters,
});
store.use(() => ({ api }));
}
});现在,我想扩大我的皮尼亚商店,以便能够使用this.api在我的actions。我试过使用文档中的所有示例
import 'pinia'
declare module 'pinia' {
export interface PiniaCustomProperties {
api: AxiosInstance;
}
}
export const useGroupStore = defineStore('groups', {
actions: {
getGroups() {
this.api <-- it is giving me undefined (or any)
}
}
})如何在我的操作中扩展我的存储以能够使用this.api?
发布于 2022-09-05 13:40:29
一个原因可能是useGroupStore()在使用store.use(() => ({ api }));的代码之前被某种方式调用。
请注意,无论您以何种方式添加到存储中,这种方式只在此后创建的存储实例中可用。如果useGroupStore()是第一位的,那么api在内部是不可见的。
您可能希望查看引导文件的顺序,并在这些方法中设置断点,以查看哪一个先运行。
https://stackoverflow.com/questions/71557584
复制相似问题