最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2023年01月06日 11:33:04   作者:老電影故事  
在vue的項目里常常會遇到父子組件間需要進行數(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ū)別總的來說他們只是存取方式的不同,兩個方法都是傳值給vuex的mutation改變state

區(qū)別

  • this.$store.commit()

同步操作

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

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

異步操作

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

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

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

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

其他使用commit即可。

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

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

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

實戰(zhàn)

  • this.$store.commit()
import Vue from "vue";
import Vuex from "vuex";
 
Vue.use(Vuex);
 
export const store = new Vuex.Store({
  // state專門用來保存 共享的狀態(tài)值
  state: {
    // 保存登錄狀態(tài)
    login: false
  },
  // mutations: 專門書寫方法,用來更新 state 中的值
  mutations: {
    // 登錄
    doLogin(state) {
      state.login = true;
    },
    // 退出
    doLogout(state) {
      state.login = false;
    }
  }
});
<script>
// 使用vux的 mapState需要引入
import { mapState } from "vuex";
 
export default {
  // 官方推薦: 給組件起個名字, 便于報錯時的提示
  name: "Header",
  // 引入vuex 的 store 中的state值, 必須在計算屬性中書寫!
  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)到首頁");
 
            // 路由跳轉(zhuǎn)指定頁面
            this.$router.push({ path: "/" });
 
            //更新 vuex 的 state的值, 必須通過 mutations 提供的方法才可以
            // 通過 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("密碼或賬號不對")
     })
    }).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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue3報錯‘defineProps‘?is?not?defined的解決方法

    Vue3報錯‘defineProps‘?is?not?defined的解決方法

    最近工作中遇到vue3中使用defineProps中報錯,飄紅,所以這篇文章主要給大家介紹了關(guān)于Vue3報錯‘defineProps‘?is?not?defined的解決方法,需要的朋友可以參考下
    2023-01-01
  • vue3+vite3+typescript實現(xiàn)驗證碼功能及表單驗證效果

    vue3+vite3+typescript實現(xiàn)驗證碼功能及表單驗證效果

    這篇文章主要介紹了vue3+vite3+typescript實現(xiàn)驗證碼功能及表單驗證效果,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-04-04
  • vue-cli和v-charts實現(xiàn)可視化圖表過程解析

    vue-cli和v-charts實現(xiàn)可視化圖表過程解析

    這篇文章主要介紹了vue-cli和v-charts實現(xiàn)可視化圖表過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-10-10
  • Vue 使用formData方式向后臺發(fā)送數(shù)據(jù)的實現(xiàn)

    Vue 使用formData方式向后臺發(fā)送數(shù)據(jù)的實現(xiàn)

    這篇文章主要介紹了Vue 使用formData方式向后臺發(fā)送數(shù)據(jù)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • antd?vue?表格rowSelection選擇框功能的使用方式

    antd?vue?表格rowSelection選擇框功能的使用方式

    這篇文章主要介紹了antd?vue?表格rowSelection選擇框功能的使用方式,具有很好的參考價值,希望對大家有所幫助。以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
    2022-12-12
  • 拿來就用vue-gird-layout組件封裝示例

    拿來就用vue-gird-layout組件封裝示例

    這篇文章主要介紹了vue-gird-layout組件封裝示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-08-08
  • Vue瀑布流插件的使用示例

    Vue瀑布流插件的使用示例

    這篇文章主要介紹了Vue瀑布流插件的使用示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-09-09
  • VUE?Element修改el-input和el-select長度的具體步驟

    VUE?Element修改el-input和el-select長度的具體步驟

    這篇文章主要給大家介紹了關(guān)于VUE?Element修改el-input和el-select長度的具體步驟,文中通過代碼介紹的非常詳細,對大家學(xué)習(xí)或者工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-12-12
  • keep-alive保持組件狀態(tài)的方法

    keep-alive保持組件狀態(tài)的方法

    這篇文章主要介紹了keep-alive保持組件狀態(tài)的方法,幫助大家更好的理解和學(xué)習(xí)vue框架,感興趣的朋友可以了解下
    2020-12-12
  • vue實現(xiàn)簽到日歷效果

    vue實現(xiàn)簽到日歷效果

    這篇文章主要為大家詳細介紹了vue實現(xiàn)簽到日歷效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08

最新評論

修武县| 灵宝市| 保亭| 辉县市| 酒泉市| 清徐县| 房产| 红桥区| 正定县| 淳化县| 灵石县| 华亭县| 花莲市| 沽源县| 三亚市| 深圳市| 青龙| 台中市| 佛冈县| 汽车| 德化县| 平江县| 麦盖提县| 迭部县| 定襄县| 元谋县| 澄江县| 双江| 通州市| 张家界市| 清流县| 大丰市| 黔东| 台南县| 高尔夫| 道孚县| 临江市| 繁昌县| 二连浩特市| 石门县| 义马市|