pinia-plugin-persistedstate icon indicating copy to clipboard operation
pinia-plugin-persistedstate copied to clipboard

In the cookie store defineNuxtRouteMiddleware method was used in nuxt3 unable to get to the store of value

Open pocketChao opened this issue 1 year ago • 3 comments

Describe the bug

In the cookie store defineNuxtRouteMiddleware method was used in nuxt3 unable to get to the store of value:

internationalStore: export const useInternationalStore = defineStore( 'international', () => { const currentLanguageCode = ref(null)

return {
  currentLanguageCode
}

}, { persist: true } ) insomeMethod: const internationalStore = useInternationalStore() internationalStore.currentLanguageCode = 'en'

when I refresh the page middleware: const nuxtApp = useNuxtApp() const internationalStore = useInternationalStore(nuxtApp.$pinia) console.log(internationalStore.currentLanguageCode) // null

Reproduction

https://codesandbox.io/p/devbox/nuxt3-m4yfyk?file=%2Fnuxt.config.ts%3A6%2C1

System Info

chorome macox16

Used Package Manager

yarn

Validations

pocketChao avatar Feb 29 '24 02:02 pocketChao

You are, in fact, able to get the value, since you are getting null which is the initial value you gave it. What you are not doing is updating this value. Furthermore, you are not really supposed to directly assign a value to a ref(), you assign to its .value property inside a setter.

sanjacob avatar Mar 04 '24 11:03 sanjacob

export const useInternationalStore = defineStore('international',
  () => {
    const currentLanguageCode = ref(null);
    function setLang(value: string) {
       currentLanguageCode.value = value;   
    }
        return {  currentLanguageCode, setLang }
    }, { persist: true }
)

in someMethod:

const internationalStore = useInternationalStore()
internationalStore.setLang('en')

sanjacob avatar Mar 04 '24 11:03 sanjacob

See #221

sanjacob avatar Mar 04 '24 11:03 sanjacob