zoukankan      html  css  js  c++  java
  • npm安装vuex及防止页面刷新数据丢失

    npm install vuex
    

    在项目scr目录下新建store文件夹,在store文件夹下新建index.js文件。

    import Vue from 'vue';
    import Vuex from 'vuex';
    Vue.use(Vuex);
    
    
    const state={
    	accountInfo:{
    
    	}
    }
    
    const mutations={
    	initUser(state,payload){
    		state.accountInfo=Object.assign(state.accountInfo,payload);
    		localStorage.setItem('accountInfo',JSON.stringify(state.accountInfo));
    	},
    	clear(state){
    		state.accountInfo={
    			
    		}
    	}
    }
    //防止页面刷新vuex中的数据丢失
    for(var item in state){
       localStorage.getItem(item)?state[item] = JSON.parse(localStorage.getItem(item)): false;
    }
    
    
    
    export default  new Vuex.Store({
        state,
        mutations
    })
    

      

  • 相关阅读:
    HTML
    JavaScript-DOM
    Visual Studio Usage
    Char 05 使用提供程序
    Char 04 运行命令
    Char 03 使用帮助系统
    char 02 初识 Powershell
    Char 01 PowerShell
    Linux Nginx
    Linux SSLH
  • 原文地址:https://www.cnblogs.com/luyuefeng/p/8334472.html
Copyright © 2011-2022 走看看