如何獲取this.$store.dispatch的返回值
獲取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)平臺'
? ? ? ? ? ? })
? ? ? ? ? } 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.js使用v-pre與v-html輸出HTML操作示例
這篇文章主要介紹了vue.js使用v-pre與v-html輸出HTML操作,結(jié)合實(shí)例形式分析了vue.js基于v-pre與v-html屬性輸出HTML的具體操作技巧,需要的朋友可以參考下2018-07-07
vue 解決在微信內(nèi)置瀏覽器中調(diào)用支付寶支付的情況
這篇文章主要介紹了vue 解決在微信內(nèi)置瀏覽器中調(diào)用支付寶支付的情況,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
Vue3中集成高德地圖并實(shí)現(xiàn)平移縮放功能
隨著前端技術(shù)的不斷發(fā)展,地圖應(yīng)用在我們的項(xiàng)目中越來越常見,本文將介紹如何在Vue3項(xiàng)目中集成高德地圖,并通過簡單的配置實(shí)現(xiàn)地圖的平移和縮放功能,需要的朋友可以參考下2024-09-09
vue+iview/elementUi實(shí)現(xiàn)城市多選
這篇文章主要介紹了vue+iview/elementUi實(shí)現(xiàn)城市多選,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
vue全局自定義指令-元素拖拽的實(shí)現(xiàn)代碼
這篇文章主要介紹了面板拖拽之vue自定義指令,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04
vue3響應(yīng)式實(shí)現(xiàn)readonly從零開始教程
這篇文章主要為大家介紹了vue3響應(yīng)式實(shí)現(xiàn)readonly從零開始教程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03

