Pinia:Official website
Pinia is a state management library. If you modify the data in the store in the component and refresh the interface, Pinia will reset the data in the store to the initial value, resulting in data loss.
Here is a solution for vue2:
You can use Pinia'sPersist
Plugin, this plugin can persist Pinia's store data to local storage, so that when you refresh the page, the data in the store will not be lost. You can refer to the documentation:document
InstallPersist
, choose the package manager you like
# yarn yarn add pinia-plugin-persist # npm npm install pinia-plugin-persist
document
import { createPinia, PiniaVuePlugin } from 'pinia' //vue2 needs to introduce PiniaVuePluginimport piniaPluginPersist from 'pinia-plugin-persist'//Introduce pinia data persistence plug-in (PiniaVuePlugin) const pinia = createPinia()//Create pinia instance(piniaPluginPersist); new Vue({ router, render: h => h(App), pinia, }).$mount('#app')
, here I am saving the article ID, so it is this file. According to the official documentation, create the file under src/stores
import { defineStore } from 'pinia' // You can name the return value of `defineStore()` arbitrarily, but it is best to use the name of the store, starting with `use` and ending with `Store`. (For example, `useUserStore`, `useCartStore`, `useProductStore`)// The first parameter is the unique ID of the Store in your application.export const useArticleIdsStore = defineStore('articleId', { // Other configurations... persist: { enabled: true,//Turn data persistence strategies: [ { key: 'articleId',//Give a name to save storage: localStorage,// localStorage storage method is local storage } ] }, state:()=>{ //The data to be saved, the initial value is 0 return {articleId:0} }, actions:{ } })
Example of usage:
import { useArticleIdsStore } from '@/stores/articleId' const articleIdsStore = useArticleIdsStore();
methods: { getArticle() { //Get the stored articleId value const articleId = ; //Other operations... }, },
This is the article about solving the problem of data loss after pinia refresh in Vue2. For more related content related to data loss after pinia refresh in Vue2, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!