[Vuejs]-Vue – Pinia Use Store Outside of Component

0👍

main.js

import { createPinia } from 'pinia'

const pinia = createPinia()

const app = createApp(App)

app.use(pinia)

//...

Replace to

import pinia from './stores/pinia_create'

const app = createApp(App)

app.use(pinia)

// move to stores/create_pinia.js
// reload page proof, persist store
//if (localStorage.getItem('state')) {
//    pinia.state.value = JSON.parse(localStorage.getItem('state'))
//}

// ...

stores/create_pinia.js

import { createPinia } from 'pinia'
const pinia = createPinia()

// reload page proof, persist store
if (localStorage.getItem('state')) {
    pinia.state.value = JSON.parse(localStorage.getItem('state'))
}
export default pinia

Leave a comment