vuex 用戶登錄信息的數據寫入與獲取


整體思路:前台獲取用戶數據,向后台發送請求,驗證成功后,改變用戶登錄狀態,前台將登錄狀態,用戶數據寫入到state中,這樣多個頁面就可以直接使用state中的用戶信息

1.向后台發送請求,若成功返回用戶名,密碼,使用 this.$store.dispatch('setLogin', true);將數據寫入到state中

頁面:login.vue
代碼:
this.loginData = await getUserInfo(this.uname,this.pwd); console.log(this.loginData); if(this.loginData.res==1){ this.$store.dispatch('setLogin', true); this.$store.dispatch('setAccount',this.loginData.obj.phone ); 

2.將數據寫到state中

頁面:action.js
代碼:
頁面: setAccount ({commit}, platform) { commit('SET_ACCOUNT', platform); }, 

3.將數據寫到state中

頁面:mutation.js
代碼:
SET_ACCOUNT (state, platform) { state.account = platform; }, 

4. 添加獲取state中對應數據方法

頁面:getter.js
代碼:
getuname: (state) => state.account, homepage.vue中使用 

5. 使用this.$store.getters.getuname調取數據

頁面:login.vue
代碼:
console.log( this.$store.getters.getuname)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM