在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ 怎樣使刷新不跳轉頁面,vuex數(shù)據(jù)也不消失

怎樣使刷新不跳轉頁面,vuex數(shù)據(jù)也不消失

目前實現(xiàn)的效果:
1.刷新 和 跳轉任何頁面都會跳到登錄頁面
2.路由攔截屏蔽后,刷新頁面vuex里的數(shù)據(jù)都消失了

問題:
1.設置路由攔截后,實現(xiàn)了不登錄全跳轉至登錄頁面,但是刷新也都跳轉到登錄頁面
2.路由攔截屏蔽后,刷新頁面vuex里的數(shù)據(jù)都消失了,知道可以存在localStorage,但是具體的怎么實現(xiàn)呢?
3.用戶數(shù)據(jù)少可以存在localStorage里,但是主頁的所有數(shù)據(jù)就不少了,難道也存在里面嗎?
4.可不可以讓刷新頁面和路由攔截結合起來,刷新頁面再請求用戶數(shù)據(jù)和當前頁面的數(shù)據(jù),具體怎么實現(xiàn)?

最終實現(xiàn)結果:刷新后依然保持在當前頁面,并且數(shù)據(jù)不消失

代碼:
index

import Vue from 'vue';
import Vuex from 'vuex';

import getters from './getters';
import actions from './actions';
import mutations from './mutations';

Vue.use(Vuex);

const state = {
  token: '',
  isLogin: false, // 用戶是否登錄
  userInfo: {  // 用戶信息
    id: '',       // 用戶id
    name: '',     // 用戶名字
    deptId: '',   //
    realName: '', // 用戶真實名字
    avatar: '',   // 用戶頭像
    roles: '',    // 用戶權限
  }
};

const store =  new Vuex.Store({
  state,
  getters,
  actions,
  mutations
});
export default store;

mutations

export const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_ISLOGIN: (state, isLogin) => {
    state.isLogin = isLogin
  },
  SET_ID: (state, id) => {
    state.userInfo.id = id
  },
  SET_NAME: (state, name) => {
    state.userInfo.name = name
  },
  SET_DEPTID: (state, deptId) => {
    state.userInfo.deptId = deptId
  },
  SET_REALNAME: (state, realName) => {
    state.userInfo.realName = realName
  },
  SET_AVATAR: (state, avatar) => {
    state.userInfo.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.userInfo.roles = roles
  },
};
export default mutations;

actions

import { login, logout, getUserInfo } from '@/api/api'
export const actions = {
  // 登錄
  Login({ commit }, userInfo) {
    return new Promise((resolve, reject) => {
      login(userInfo).then(response => {
        let token = response.data.token;
        localStorage.setItem('token', token);
        commit('SET_TOKEN', token);
        commit('SET_ISLOGIN', true);
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 獲取用戶信息
  GetUserInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getUserInfo(state.token).then(response => {
        if (!response.data) {
          reject('error')
        }
        let data = response.data;
        console.log(response.data);
        commit('SET_ID', data.Id);
        commit('SET_NAME', data.Name);
        commit('SET_DEPTID', data.DeptId);
        commit('SET_REALNAME', data.RealName);
        commit('SET_AVATAR', data.Face);
        commit('SET_ROLES', data.Roles);
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 登出
  LogOut({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        commit('SET_TOKEN', '');
        commit('SET_ISLOGIN', false);
        localStorage.removeItem('token');
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
};
export default actions;

main.js

import Vue from 'vue';
import iView from 'iview';
import App from './App';
import 'iview/dist/styles/iview.css';
import router from './router';
import store from './store';

import { getUserInfo } from '@/api/api'

Vue.config.productionTip = false
Vue.use(iView);

// 路由攔截
router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {
    if (!store.state.isLogin && to.path !== '/login') {
      next({
        path: '/login',
      })
    } else {
      next();
    }
  }
  else {
    next();
  }
});

new Vue({
  el: '#app',
  router,
  store,
  template: '<App/>',
  components: { App }
});

求大神幫忙!!!

回答
編輯回答
挽青絲

sessionStorage 頁面刷新不消失 關閉瀏覽器消失
localStorage 瀏覽器關閉不消失
只要存儲登錄狀態(tài)
根據(jù)需求看用哪個
可以找下vuex 插件 關于localStorage 的 插件很簡單 改成sessionStorage 只要換個名字

2018年9月3日 19:32
編輯回答
夢囈

vuex 的持久化組件 vuex-persistedstate;

使用方法

import createPersistedState from 'vuex-persistedstate';

export default new Vuex.Store({

    plugins: [createPersistedState()]
})
2018年3月8日 04:19
編輯回答
掛念你

vuex有個插件,就是把vuex數(shù)據(jù)自動保存到本地存儲,自動取值,很方便,忘了叫什么了

2018年7月7日 23:10
編輯回答
忘了我

使用把登錄狀態(tài)儲存在sessionStorage中方法解決的,鏈接描述

2017年3月5日 11:49
編輯回答
青裙

刷新代表就是重啟項目,數(shù)據(jù)肯定會丟失
給你提供倆種給用戶錯覺感是沒有丟失的方案:

1 純前端實現(xiàn) / 也就是 本地存儲, sessionStorage,和localStorage  具體不介紹了 百度一堆,好處就是
實現(xiàn)簡單 無非set get倆個方法存取,壞處 萬一本地存儲滿了,或者刪了 或者導致用戶體驗卡了等等一系列問題

2 前后端配合,寫倆個接口,一個是存一個是獲取,刷新前需要請求接口把數(shù)據(jù)存進去,刷新之后把數(shù)據(jù)拿回來
這個比較繁瑣,但是不會出現(xiàn)較大的問題 **=bug**

看你需求 看你服務的人群 看你產(chǎn)品的態(tài)度 綜合考慮

2017年3月24日 21:36
編輯回答
朕略萌

我寫的一個 vuex 插件 能解決你的 需求
https://github.com/qinouz/vue... 如果好用 給個star

2017年9月25日 21:25
編輯回答
安于心

我說一下我的想法,希望對你有所幫助!
1、刷新相當與重啟項目,之前獲取到的數(shù)據(jù)也只是通過store暫存起來,項目關閉時就不見了,這有些像電腦重啟,存儲在RAM的數(shù)據(jù)會消失。
2、既然是刷新,用localStorage的話你關閉瀏覽器或者標簽數(shù)據(jù)還在,這樣用戶體驗不好,而且隨著數(shù)據(jù)增多,沒有及時刪除的話,會導致更新項目的時候以前的數(shù)據(jù)沖突,這時候最好是使用sessionStorage,和localStorage差不多,差別在于關閉標簽或者瀏覽器時數(shù)據(jù)就會自動清除。
3、對于提到的前面主頁保存的數(shù)據(jù),當然也要保存到sessionStorage中,雖然保存的大小有限制,但是一般項目數(shù)據(jù)絕對是夠裝的。
4、項目加載時,可以在created先進行判斷sessionStorage是否存在對應的數(shù)據(jù),有的話,將數(shù)據(jù)commitstore中去,沒有就正常加載,發(fā)送請求來請求數(shù)據(jù)等一系列操作。

2018年6月25日 07:53
編輯回答
乖乖瀦

簡單回答一下,因為我們的項目也有類似的需求,并被我們的老大整理完畢了
首先localStorage是vue.js我理解是vue.js中的全局變量,并且會記錄在緩存中,刷新不會丟失,使用起來也很簡單,localStorage.user_id='001'進行存儲,localStorage.user_id取值。
其次就是localStorage的定義,我們不可以完全依賴他,就像你說的,不可能將主頁數(shù)據(jù)都存在里面,那你這個頁面維護起來都是問題。我對他的定義是,存儲必要的,刷新不能丟失的主鍵類數(shù)據(jù),比如user_id。
而用vuex管理的store,他的機制就是在頁面中刷新就釋放的。

到這里,我簡單說一說我們是怎么做的,僅供參考。
我們的一些用戶信息相關的對象,存儲在store中進行管理,并且在localStorage中存儲user_id以及token。
首先在main.js中創(chuàng)建路由攔截,判斷to的頁面是否需要登錄權限,如果需要登錄權限,優(yōu)先判斷store中是否有登錄狀態(tài)碼,如果沒有,有可能是刷新頁面導致的,去判斷l(xiāng)ocalStorage中是否有token、user_id,如果沒有,直接返回登錄頁。如果localStorage中有相關信息,那么好,我們在這里直接進行一次查詢,將用戶相關信息再次寫入store中存儲。

僅供參考,思路捋清楚了,該怎么做也就一目了然了。

2017年4月12日 13:32
編輯回答
萌二代
  • 登錄問題:

    1、登錄時叫后端把登錄狀態(tài)放在cookie,并且只讀且設置數(shù)據(jù)超時。
    2、調后端api去獲取登錄狀態(tài)。
    如果前端自己放在cookie、sessionStorage、localStorage,那只要f12 修改一下登錄
    狀態(tài)的值就可以跳過攔截。

  • 主頁里的其他數(shù)據(jù)在刷新后都應該重新調api獲取。用緩存的話,需要考慮數(shù)據(jù)超時、數(shù)據(jù)有效性的問題,并且用戶刷新頁面不就是希望頁面能夠重新獲取。
2018年2月21日 23:12