亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

如何獲取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實(shí)現(xiàn)組件通信的方法

    本文將介紹幾種常見的?Vue?3?組件通信方法,包括?props、emits、provide?和?inject、事件總線以及?Vuex?狀態(tài)管理,需要的朋友可以參考下
    2024-07-07
  • JS數(shù)組Array常用方法匯總+實(shí)例

    JS數(shù)組Array常用方法匯總+實(shí)例

    這篇文章主要介紹了JS數(shù)組Array常用方法匯總和實(shí)例,JavaScript創(chuàng)建數(shù)組的方式,找數(shù)組元素,數(shù)組去重,實(shí)現(xiàn)數(shù)組排序需要的朋友可以參考下
    2022-05-05
  • vue 兄弟組件的信息傳遞的方法實(shí)例詳解

    vue 兄弟組件的信息傳遞的方法實(shí)例詳解

    這篇文章主要介紹了vue 兄弟組件的信息傳遞的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-08-08
  • Vue實(shí)現(xiàn)點(diǎn)擊時(shí)間獲取時(shí)間段查詢功能

    Vue實(shí)現(xiàn)點(diǎn)擊時(shí)間獲取時(shí)間段查詢功能

    這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)點(diǎn)擊時(shí)間獲取時(shí)間段查詢功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • 函數(shù)式組件劫持替代json封裝element表格

    函數(shù)式組件劫持替代json封裝element表格

    這篇文章主要介紹了為大家函數(shù)式組件劫持替代json封裝element表格的過程思路及示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • 推薦一款簡(jiǎn)易的solid?js消息UI庫(kù)使用詳解

    推薦一款簡(jiǎn)易的solid?js消息UI庫(kù)使用詳解

    這篇文章主要為大家介紹了推薦一款簡(jiǎn)易的solid-js消息UI庫(kù)使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • Sublime Text新建.vue模板并高亮(圖文教程)

    Sublime Text新建.vue模板并高亮(圖文教程)

    這篇文章主要介紹了Sublime Text新建.vue模板并高亮(圖文教程),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-10-10
  • Vue2.x安裝并使用SCSS的全部過程

    Vue2.x安裝并使用SCSS的全部過程

    這篇文章主要給大家介紹了關(guān)于Vue2.x安裝并使用SCSS的相關(guān)資料,以及如何在vue 2.x中全局引用公共scss文件,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-03-03
  • vue中的v-model原理,與組件自定義v-model詳解

    vue中的v-model原理,與組件自定義v-model詳解

    這篇文章主要介紹了vue中的v-model原理,與組件自定義v-model詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • Vue使用Vuex一步步封裝并使用store全過程

    Vue使用Vuex一步步封裝并使用store全過程

    這篇文章主要介紹了Vue使用Vuex一步步封裝并使用store全過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01

最新評(píng)論