nuxt after refresh the page to prevent the loss of value store

1, configure the plug ()

plugins: [
    {src:'~/plugins/storeCache',ssr: false},
  ],

 

Note: To disable the server is running, or will be error, this event is added to the client, not on the server add a small rendering time.

2 . In plugins / storeCache.js

Write the code as follows.

Export default  function (CTX) {
     // before leaving the page to refresh the data stored in the store to the session 
    window.addEventListener ( 'beforeunload', () => { 
        sessionStorage.setItem ( "storeCache" , the JSON.stringify (ctx.store. State)) 
    }); 
    // page loaded in the session store data 
    window.addEventListener ( 'load', () => { 
        the let storeCache = sessionStorage.getItem ( "storeCache" )
         IF (storeCache) {
             // the session Alternatively to store data in the store 
            ctx.store.replaceState (the JSON.parse (storeCache)); 
        } 
    }); 
}

 

Done

 

 

 

 

Guess you like

Origin www.cnblogs.com/fqh123/p/12547848.html