You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Check this box if you encountered the issue using Nuxt and the included module.
Describe the bug
axios.js
// it cannot workconstuserStore=useUserStore(pinia);axios.interceptors.request.use((request)=>{// in here, it can work// const userStore = useUserStore(pinia);if(userStore.user){console.log('axios request: ',userStore.user);}returnrequest;},(error)=>Promise.reject(error));
I know it can move into axios.interceptors.request inner, but useUserStore(pinia) can be work, why pinia-plugin-persistedstate cannot. I think it maybe a bug.
And I just reproduced the problem using Axios. In fact, in my project, there are many places where the store is created before Pinia. All of them use the way of useUserStore(pinia). If modifications are to be made, this change will be very large.
Hi, thanks for the reproduction, it helped me pin what is probably the issue.
I think it comes from the way Pinia itself handles plugins. As stated in the docs (here), Pinia plugins are applied ONLY after pinia is passed to the app. In your case, Pinia is instantiated and used before the app (you need to pass it the Pinia instance explicitely), so the persistedstate plugin is not part of it because of that.
It can be confirmed by this code from pinia, which defers plugins if there is no Vue app instance available.
I will try to investigate a bit if that can be fixed/worked around from our plugin, but I doubt it.
I can see two solutions for you:
Avoiding calling pinia stores outside of app context/before app is instantiated so we are sure the plugin is installed.
Not use the plugin and rely on other solutions like vueuse's useLocalStorage as suggested here
Are you using Nuxt?
Describe the bug
axios.js
main.js
app.vue
Got error
I know it can move into
axios.interceptors.request
inner, butuseUserStore(pinia)
can be work, whypinia-plugin-persistedstate
cannot. I think it maybe a bug.And I just reproduced the problem using Axios. In fact, in my project, there are many places where the store is created before Pinia. All of them use the way of
useUserStore(pinia)
. If modifications are to be made, this change will be very large.Reproduction
https://stackblitz.com/edit/vitejs-vite-tgnzng?file=src%2Faxios.js
System Info
Used Package Manager
npm
Validations
The text was updated successfully, but these errors were encountered: