Vuex中mutations和actions的區(qū)別及說明
mutation
我們知道,在使用vuex對項(xiàng)目狀態(tài)進(jìn)行管理時(shí),只能使用commit來提交mutation對store中的狀態(tài)進(jìn)行更改
Vuex 中的 mutation 非常類似于事件:每個(gè) mutation 都有一個(gè)字符串的 事件類型 (type) 和 一個(gè) 回調(diào)函數(shù) (handler)。這個(gè)回調(diào)函數(shù)就是我們實(shí)際進(jìn)行狀態(tài)更改的地方,并且它會接受 state 作為第一個(gè)參數(shù):
const store = new Vuex.Store({
? state: {
? ? count: 1
? },
? mutations: {
? ? increment (state) {
? ? ? // 變更狀態(tài)
? ? ? state.count++
? ? }
? }
})
//你不能直接調(diào)用一個(gè) mutation handler。這個(gè)選項(xiàng)更像是事件注冊:“當(dāng)觸發(fā)一個(gè)類型為 increment 的 mutation 時(shí),調(diào)用此函數(shù)?!币獑拘岩粋€(gè) mutation handler,你需要以相應(yīng)的 type 調(diào)用 store.commit 方法:
store.commit('increment') ??Mutation 必須是同步函數(shù)
mutations: {
? someMutation (state) {
? ? api.callAsyncMethod(() => {
? ? ? state.count++
? ? })
? }
}我們注意上面這段代碼,在mutation里面加入了異步處理的函數(shù)。
其實(shí)mutation是可以正常使用的,但是我們在日常的開發(fā)中debug的時(shí)候,我們需要查看devtool中的mutation日志。
理論上來說,是mutation走一步,devtool記錄一步,但是在mutation中加入異步函數(shù)就會導(dǎo)致我們devtool的記錄失敗,因?yàn)閐evtool不知道你里面的異步函數(shù)什么時(shí)候調(diào)用,在哪里調(diào)用
Action
Action 類似于 mutation,不同在于:
Action 提交的是 mutation,而不是直接變更狀態(tài)。
Action 可以包含任意異步操作。
const store = new Vuex.Store({
? state: {
? ? count: 0
? },
? mutations: {
? ? increment (state) {
? ? ? state.count++
? ? }
? },
? actions: {
? ? increment (context) {
? ? ? context.commit('increment')
? ? }
? }
})Action 函數(shù)接受一個(gè)與 store 實(shí)例具有相同方法和屬性的 context 對象,因此你可以調(diào)用 context.commit 提交一個(gè) mutation,或者通過 context.state 和 context.getters 來獲取 state 和 getters。
實(shí)踐中,我們會經(jīng)常用到 ES2015 的 參數(shù)解構(gòu) (opens new window)來簡化代碼(特別是我們需要調(diào)用 commit 很多次的時(shí)候):
actions: {
? increment ({ commit }) {
? ? commit('increment')
? }
}在實(shí)際開發(fā)的store文件中
// src/store/index.js
import Vue from 'vue';
import Vuex from '@/vuex';
Vue.use(Vuex);
const store = new Vuex.Store({
? state: {
? ? num: 10
? },
? getters: {
? ? getPrice(state) {
? ? ? return state.num * 10
? ? }
? },
? // 同步更新狀態(tài)
import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
const user = {
? state: {
? ? token: getToken(),
? ? name: '',
? ? avatar: '',
? ? roles: [],
? ? permissions: []
? },
//同步方法
? mutations: {
? ? SET_TOKEN: (state, token) => {
? ? ? state.token = token
? ? },
? ? SET_NAME: (state, name) => {
? ? ? state.name = name
? ? },
? ? SET_AVATAR: (state, avatar) => {
? ? ? state.avatar = avatar
? ? },
? ? SET_ROLES: (state, roles) => {
? ? ? state.roles = roles
? ? },
? ? SET_PERMISSIONS: (state, permissions) => {
? ? ? state.permissions = permissions
? ? }
? },
//異步方法
? actions: {
? ? // 登錄(使用單點(diǎn)登錄此處就作廢)
? ? Login({ commit }, userInfo) {
? ? ? const loginType = userInfo.loginType
? ? ? const tentantCode = userInfo.tentantCode
? ? ? const username = userInfo.username.trim()
? ? ? const password = userInfo.password
? ? ? const code = userInfo.code
? ? ? const uuid = userInfo.uuid
? ? ? return new Promise((resolve, reject) => {
? ? ? ? login(loginType, tentantCode, username, password, code, uuid).then(res => {
? ? ? ? ? setToken(res.token)
? ? ? ? ? commit('SET_TOKEN', res.token)
? ? ? ? ? resolve()
? ? ? ? }).catch(error => {
? ? ? ? ? reject(error)
? ? ? ? })
? ? ? })
? ? },
? ? // 獲取用戶信息
? ? GetInfo({ commit, state }) {
? ? ? return new Promise((resolve, reject) => {
? ? ? ? getInfo().then(res => {
? ? ? ? ? if (res.data.rolePermission
? ? ? ? ? ? && res.data.rolePermission
? ? ? ? ? ? > 0) { // 驗(yàn)證返回的roles是否是一個(gè)非空數(shù)組
? ? ? ? ? ? commit('SET_ROLES', res.roles)
? ? ? ? ? ? commit('SET_PERMISSIONS', res.permissions)
? ? ? ? ? } else {
? ? ? ? ? ? commit('SET_ROLES', ['ROLE_DEFAULT'])
? ? ? ? ? }
? ? ? ? ? commit('SET_NAME', res.data.nickName
? ? ? ? ? )
? ? ? ? ? commit('SET_AVATAR', res.data.avatar)
? ? ? ? ? resolve(res)
? ? ? ? }).catch(error => {
? ? ? ? ? reject(error)
? ? ? ? })
? ? ? })
? ? },
? ? // 退出系統(tǒng)
? ? LogOut({ commit, state }) {
? ? ? return new Promise((resolve, reject) => {
? ? ? ? logout(state.token).then(() => {
? ? ? ? ? commit('SET_TOKEN', '')
? ? ? ? ? commit('SET_ROLES', [])
? ? ? ? ? commit('SET_PERMISSIONS', [])
? ? ? ? ? removeToken()
? ? ? ? ? resolve()
? ? ? ? }).catch(error => {
? ? ? ? ? reject(error)
? ? ? ? })
? ? ? })
? ? },
? ? // 前端 登出
? ? FedLogOut({ commit }) {
? ? ? return new Promise(resolve => {
? ? ? ? commit('SET_TOKEN', '')
? ? ? ? removeToken()
? ? ? ? resolve()
? ? ? })
? ? }
? }
}
export default user比如我們在登錄的時(shí)候需要觸發(fā)store中的方法
<template>
? <div>單點(diǎn)登錄頁面</div>
</template>
<script>
import {
? doLoginByTicket,
? getInfo,
? isLogin,
? getSsoAuthUrl,
? getRouter,
} from "../api/login";
import { getToken, setToken } from "@/utils/auth";
export default {
? name: "Screenfull",
? data() {
? ? return {};
? },
? created() {
? ? this.checkIsLogin();
? },
? methods: {
? ? checkIsLogin() {
? ? ? isLogin().then((res) => {
? ? ? ? if (res.data == true) {
? ? ? ? ? //獲取用戶信息;
? ? ? ? ? console.log("isLogin", res);
? ? ? ? ? // this.$router.push("/");
? ? ? ? } else {
? ? ? ? ? //獲取請求進(jìn)來的完整url
? ? ? ? ? let url = window.location.href;
? ? ? ? ? if (url.indexOf("ticket=") < 0) {
? ? ? ? ? ? //如果沒有ticket
? ? ? ? ? ? getSsoAuthUrl({ clientLoginUrl: url }).then((res) => {
? ? ? ? ? ? ? window.location.href = res.data;
? ? ? ? ? ? });
? ? ? ? ? ? return;
? ? ? ? ? }
? ? ? ? ? let tstr = url
? ? ? ? ? ? .substring(url.indexOf("?") + 1)
? ? ? ? ? ? .split("=")[1]
? ? ? ? ? ? .split("#")[0]; //先截取url的?后面的參數(shù)部分,在根據(jù)&分割成參數(shù)數(shù)組
? ? ? ? ? doLoginByTicket({ ticket: tstr }).then((res) => {
? ? ? ? ? ? if (res.code == 200) {
? ? ? ? ? ? ? setToken(res.data);
? ? ? ? ? ? ? getInfo().then((res) => {
? ? ? ? ? ? ? ? if (res.data.rolePermission) {
? ? ? ? ? ? ? ? //觸發(fā)mutations同步方法
? ? ? ? ? ? ? ? ? this.$store.commit("SET_ROLES", ["admin"]);
? ? ? ? ? ? ? ? ? this.$store.commit("SET_PERMISSIONS", ["*:*:*"]);
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? commit("SET_ROLES", ["ROLE_DEFAULT"]);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? this.$store.commit("SET_NAME", res.data.nickName);
? ? ? ? ? ? ? });
? ? ? ? ? ? ? getRouter().then(() => {
?? ??? ??? ??? ?//觸發(fā)actions異步方法
? ? ? ? ? ? ? ? this.$store.dispatch("GenerateRoutes");
? ? ? ? ? ? ? ? window.location.reload();
? ? ? ? ? ? ? });
? ? ? ? ? ? } else {
? ? ? ? ? ? ? console.log("檢查票據(jù)失敗");
? ? ? ? ? ? }
? ? ? ? ? });
? ? ? ? }
? ? ? });
? ? },
? },
};
</script>
<style lang="scss" scoped></style>總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
element?ui中el-form-item的屬性rules的用法示例小結(jié)
這篇文章主要介紹了element?ui中el-form-item的屬性rules的用法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2024-07-07
解決vue v-for 遍歷循環(huán)時(shí)key值報(bào)錯(cuò)的問題
今天小編就為大家分享一篇解決vue v-for 遍歷循環(huán)時(shí)key值報(bào)錯(cuò)的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
詳解vue3結(jié)合ts項(xiàng)目中使用mockjs
這篇文章主要為大家介紹了vue3結(jié)合ts項(xiàng)目中使用mockjs示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
vue3簡易實(shí)現(xiàn)proxy代理實(shí)例詳解
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)學(xué)生成績管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03
Vuex中mutations與actions的區(qū)別詳解
下面小編就為大家分享一篇Vuex中mutations與actions的區(qū)別詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03
vue生成二維碼QR?Code的簡單實(shí)現(xiàn)方法示例
這篇文章主要為大家介紹了vue生成二維碼QR?Code的實(shí)現(xiàn)示例詳情,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04
詳解Vue template 如何支持多個(gè)根結(jié)點(diǎn)
這篇文章主要介紹了詳解Vue template 如何支持多個(gè)根結(jié)點(diǎn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02

