如何獲取this.$store.dispatch的返回值
更新時(shí)間:2023年01月10日 10:54:55 作者:三分惡
這篇文章主要介紹了如何獲取this.$store.dispatch的返回值問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
獲取this.$store.dispatch的返回值
this.$store.dispatch() 是用來傳值給vuex的mutation改變state。
我們來看看怎么獲取this.$store.dispatch() 調(diào)用的返回值。
Action
首先看看定義的Action:
? login({ commit }, userInfo) {
? ? // const { username, password } = userInfo
? ? return new Promise((resolve, reject) => {
? ? ? login(userInfo).then(response => {
? ? ? ? const { data } = response
? ? ? ? commit('SET_TOKEN', data.token)
? ? ? ? setToken(data.token)
? ? ? ? resolve(response)
? ? ? }).catch(error => {
? ? ? ? reject(error)
? ? ? })
? ? })
? },兩個(gè)關(guān)鍵點(diǎn):
- 返回一個(gè)new Promise
return new Promise((resolve, reject)
- resolve函數(shù)中傳入返回值
resolve(response)
調(diào)用
? ? ? ? ? ? this.$store.dispatch('user/login', this.loginForm)
? ? ? ? ? ? ? .then(res => {
? ? ? ? ? ? ? ? console.log(res)
? ? ? ? ? ? ? ? fullLoading.close();
? ? ? ? ? ? ? ? //登陸首頁還是之前訪問需要重定向的地址
? ? ? ? ? ? ? ? this.$router.push({
? ? ? ? ? ? ? ? ? path: this.redirect || '/'
? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? this.loading = false
? ? ? ? ? ? ? })
? ? ? ? ? ? ? .catch(error => {}在調(diào)用里就可以直接通過 res 來直接獲取返回值了。
? ? ? ? ? ? ? .then(res => {
? ? ? ? ? ? ? ? console.log(res)根據(jù)vuex的this.$store.dispatch()返回值處理邏輯
App.vue
? ? ? ? const ret = await this.$store.dispatch('userLogin', {
? ? ? ? ? username: this.curUserName,
? ? ? ? ? password: this.curPassword
? ? ? ? })
? ? ? ? if (ret && ret.info) {
? ? ? ? ? this.$message.success(ret.info)
? ? ? ? ? await this.$store.dispatch('controlLoginDialog', false)
? ? ? ? } else {
? ? ? ? ? this.$message.warning(ret)
? ? ? ? }vuex/store/action.js
? async userLogin ({commit}, account) {
? ? let userInfo = {}
? ? return new Promise((resolve, reject) => {
? ? ? requestUserLogin(account).then(response => {
? ? ? ? if (response.status === 200) {
? ? ? ? ? if (response.data.data) {
? ? ? ? ? ? userInfo = response.data.data
? ? ? ? ? ? userInfo.userName = userInfo.name
? ? ? ? ? ? userInfo.isLogin = true
? ? ? ? ? ? resolve({
? ? ? ? ? ? ? info: userInfo.userName + ' 登錄成功,歡迎進(jìn)入百度云智學(xué)院實(shí)驗(yàn)平臺(tái)'
? ? ? ? ? ? })
? ? ? ? ? } else if (response.data.fail) {
? ? ? ? ? ? userInfo.userName = ''
? ? ? ? ? ? userInfo.isLogin = false
? ? ? ? ? ? myConsole('response.data.fail')
? ? ? ? ? ? resolve(response.data.fail)
? ? ? ? ? }
? ? ? ? } else {
? ? ? ? ? userInfo.userName = ''
? ? ? ? ? userInfo.isLogin = false
? ? ? ? }
? ? ? ? commit(USER_LOGIN, {userInfo})
? ? ? }).catch(err => {
? ? ? ? myConsole(err)
? ? ? ? reject(err)
? ? ? })
? ? })
? },總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
深入理解?Vue?3實(shí)現(xiàn)組件通信的方法
本文將介紹幾種常見的?Vue?3?組件通信方法,包括?props、emits、provide?和?inject、事件總線以及?Vuex?狀態(tài)管理,需要的朋友可以參考下2024-07-07
Vue實(shí)現(xiàn)點(diǎn)擊時(shí)間獲取時(shí)間段查詢功能
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)點(diǎn)擊時(shí)間獲取時(shí)間段查詢功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
推薦一款簡(jiǎn)易的solid?js消息UI庫使用詳解
這篇文章主要為大家介紹了推薦一款簡(jiǎn)易的solid-js消息UI庫使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
vue中的v-model原理,與組件自定義v-model詳解
這篇文章主要介紹了vue中的v-model原理,與組件自定義v-model詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08

