欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vuex中this.$store.commit和this.$store.dispatch的基本用法實(shí)例

 更新時(shí)間:2023年01月06日 11:33:04   作者:老電影故事  
在vue的項(xiàng)目里常常會(huì)遇到父子組件間需要進(jìn)行數(shù)據(jù)傳遞的情況,下面這篇文章主要給大家介紹了關(guān)于vuex中this.$store.commit和this.$store.dispatch的基本用法的相關(guān)資料,需要的朋友可以參考下

前言

this. s t o r e . d i s p a t c h ( ) 與 t h i s . store.dispatch() 與 this. store.dispatch()與this.store.commit()方法的區(qū)別總的來(lái)說(shuō)他們只是存取方式的不同,兩個(gè)方法都是傳值給vuex的mutation改變state

區(qū)別

  • this.$store.commit()

同步操作

this.$store.commit('方法名',值)【存儲(chǔ)】

this.$store.state.方法名【取值】
  • this.$store.dispatch()

異步操作

this.$store.dispatch('方法名',值)【存儲(chǔ)】

this.$store.getters.方法名【取值】

當(dāng)操作行為中含有異步操作:

比如向后臺(tái)發(fā)送請(qǐng)求獲取數(shù)據(jù),就需要使用action的dispatch去完成了。

其他使用commit即可。

commit => mutations,用來(lái)觸發(fā)同步操作的方法。

dispatch =>actions,用來(lái)觸發(fā)異步操作的方法。

在store中注冊(cè)了mutation和action,在組件中用dispatch調(diào)用action,然后action用commit調(diào)用mutation。

實(shí)戰(zhàn)

  • this.$store.commit()
import Vue from "vue";
import Vuex from "vuex";
 
Vue.use(Vuex);
 
export const store = new Vuex.Store({
  // state專(zhuān)門(mén)用來(lái)保存 共享的狀態(tài)值
  state: {
    // 保存登錄狀態(tài)
    login: false
  },
  // mutations: 專(zhuān)門(mén)書(shū)寫(xiě)方法,用來(lái)更新 state 中的值
  mutations: {
    // 登錄
    doLogin(state) {
      state.login = true;
    },
    // 退出
    doLogout(state) {
      state.login = false;
    }
  }
});
<script>
// 使用vux的 mapState需要引入
import { mapState } from "vuex";
 
export default {
  // 官方推薦: 給組件起個(gè)名字, 便于報(bào)錯(cuò)時(shí)的提示
  name: "Header",
  // 引入vuex 的 store 中的state值, 必須在計(jì)算屬性中書(shū)寫(xiě)!
  computed: {
    // mapState輔助函數(shù), 可以快速引入store中的值
    // 此處的login代表,  store文件中的 state 中的 login, 登錄狀態(tài)
    ...mapState(["login"])
  },
  methods: {
    logout() {
      this.$store.commit("doLogout");
    }
  }
};
</script>


<script>
export default {
  name: "Login",
  data() {
    return {
      uname: "",
      upwd: ""
    };
  },
  methods: {
    doLogin() {
      console.log(this.uname, this.upwd);
      let data={
        uname:this.uname,
        upwd:this.upwd
      }
       this.axios
        .post("user_login.php", data)
        .then(res => {
          console.log(res);
          let code = res.data.code;
 
          if (code == 1) {
            alert("恭喜您, 登錄成功! 即將跳轉(zhuǎn)到首頁(yè)");
 
            // 路由跳轉(zhuǎn)指定頁(yè)面
            this.$router.push({ path: "/" });
 
            //更新 vuex 的 state的值, 必須通過(guò) mutations 提供的方法才可以
            // 通過(guò) commit('方法名') 就可以出發(fā) mutations 中的指定方法
            this.$store.commit("doLogin");
          } else {
            alert("很遺憾, 登陸失敗!");
          }
        })
        .catch(err => {
          console.error(err);
        });
    }
 
  }
};
</script>
  • this.$store.dispatch()
toggleSideBar() {
      this.$store.dispatch('app/toggleSideBar')
    },
    async logout() {
      await this.$store.dispatch('user/logout')
      this.$router.push(`/login?redirect=${this.$route.fullPath}`)
    }
const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_INTRODUCTION: (state, introduction) => {
    state.introduction = introduction
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  }
}
const actions = {
  // user login
  login({ commit }, userInfo){
    const { username, password,useraccount} = userInfo;
    return new Promise((resolve, reject) => {
      login(({userName:username,userAccount:useraccount,userPassword:password})).then(response=>{
        const data=response;
        commit('SET_TOKEN', 1)
        setToken(null)
        commit('SET_ROLES', 1)
        commit('SET_NAME', 1)
        commit('SET_AVATAR', 1)
        commit('SET_INTRODUCTION', 1)
        resolve()
      }).catch(error => {
       reject(error)
      //  debugger;
        //  $Message("密碼或賬號(hào)不對(duì)")
     })
    }).catch(error => {
      reject(error)
    })
  }
}

總結(jié)

到此這篇關(guān)于vuex中this.$store.commit和this.$store.dispatch基本用法的文章就介紹到這了,更多相關(guān)vuex this.$store.commit和this.$store.dispatch用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue.js3.2的vnode部分優(yōu)化升級(jí)使用示例詳解

    Vue.js3.2的vnode部分優(yōu)化升級(jí)使用示例詳解

    這篇文章主要為大家介紹了Vue.js3.2的vnode部分優(yōu)化升級(jí)使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • 實(shí)例分析編寫(xiě)vue組件方法

    實(shí)例分析編寫(xiě)vue組件方法

    在本篇文章中我們給大家總結(jié)了關(guān)于編寫(xiě)vue組件的方法知識(shí)點(diǎn),有此需要的讀者們跟著學(xué)習(xí)下。
    2019-02-02
  • Vue中splice()方法對(duì)數(shù)組進(jìn)行增刪改等操作的實(shí)現(xiàn)

    Vue中splice()方法對(duì)數(shù)組進(jìn)行增刪改等操作的實(shí)現(xiàn)

    vue中對(duì)數(shù)組的元素進(jìn)行刪除,以前一直以為這個(gè)方法是vue中特有的,后來(lái)百度之后才知道原來(lái)是js的一個(gè)寫(xiě)法,下面這篇文章主要給大家介紹了關(guān)于Vue中splice()方法對(duì)數(shù)組進(jìn)行增刪改等操作的實(shí)現(xiàn)方法,需要的朋友可以參考下
    2023-05-05
  • vue 微信掃碼登錄(自定義樣式)

    vue 微信掃碼登錄(自定義樣式)

    這篇文章主要介紹了vue 微信掃碼登錄(自定義樣式),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • VUE多層路由嵌套實(shí)現(xiàn)代碼

    VUE多層路由嵌套實(shí)現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了VUE多層路由嵌套的實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • vue使用recorder-core.js實(shí)現(xiàn)錄音功能

    vue使用recorder-core.js實(shí)現(xiàn)錄音功能

    這篇文章主要介紹了vue如何使用recorder-core.js實(shí)現(xiàn)錄音功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-07-07
  • vue實(shí)現(xiàn)文件上傳

    vue實(shí)現(xiàn)文件上傳

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)文件上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • 在Linux服務(wù)器上部署vue項(xiàng)目

    在Linux服務(wù)器上部署vue項(xiàng)目

    這篇文章介紹了在Linux服務(wù)器上部署vue項(xiàng)目的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-11-11
  • Vue中對(duì)比scoped css和css module的區(qū)別

    Vue中對(duì)比scoped css和css module的區(qū)別

    這篇文章主要介紹了Vue中scoped css和css module的區(qū)別對(duì)比,scoped css可以直接在能跑起來(lái)的vue項(xiàng)目中使用而css module需要增加css-loader配置才能生效。具體內(nèi)容詳情大家參考下本文
    2018-05-05
  • vue項(xiàng)目中更改名字和圖標(biāo)的簡(jiǎn)單實(shí)現(xiàn)步驟

    vue項(xiàng)目中更改名字和圖標(biāo)的簡(jiǎn)單實(shí)現(xiàn)步驟

    今天在寫(xiě)vue項(xiàng)目時(shí)碰到的問(wèn)題是怎么修改vue的網(wǎng)頁(yè)圖標(biāo),所以下面這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目中更改名字和圖標(biāo)的簡(jiǎn)單實(shí)現(xiàn),文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08

最新評(píng)論