Vuex中mutations和actions的區(qū)別及說明
mutation
我們知道,在使用vuex對(duì)項(xiàng)目狀態(tài)進(jìn)行管理時(shí),只能使用commit來提交mutation對(duì)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)更改的地方,并且它會(huì)接受 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)更像是事件注冊(cè):“當(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是可以正常使用的,但是我們?cè)谌粘5拈_發(fā)中debug的時(shí)候,我們需要查看devtool中的mutation日志。
理論上來說,是mutation走一步,devtool記錄一步,但是在mutation中加入異步函數(shù)就會(huì)導(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 對(duì)象,因此你可以調(diào)用 context.commit 提交一個(gè) mutation,或者通過 context.state 和 context.getters 來獲取 state 和 getters。
實(shí)踐中,我們會(huì)經(jīng)常用到 ES2015 的 參數(shù)解構(gòu) (opens new window)來簡(jiǎn)化代碼(特別是我們需要調(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比如我們?cè)诘卿浀臅r(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 {
? ? ? ? ? //獲取請(qǐng)求進(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)文章
Vue使用new Image()實(shí)現(xiàn)圖片預(yù)加載功能
這篇文章主要介紹了如何在 Vue 中實(shí)現(xiàn)圖片預(yù)加載的一個(gè)簡(jiǎn)單小demo以及優(yōu)化方案,文中通過代碼示例介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-11-11
基于Vue.js實(shí)現(xiàn)一個(gè)完整的登錄功能
在現(xiàn)代Web應(yīng)用中,用戶登錄功能是一個(gè)核心模塊,它不僅涉及到用戶身份驗(yàn)證,還需要處理表單驗(yàn)證、狀態(tài)管理、接口調(diào)用等多個(gè)環(huán)節(jié),本文將基于一個(gè)Vue.js項(xiàng)目中的登錄功能實(shí)現(xiàn),深入解析其背后的技術(shù)細(xì)節(jié),幫助開發(fā)者更好地理解和實(shí)現(xiàn)類似功能,需要的朋友可以參考下2025-02-02
詳解VS Code使用之Vue工程配置format代碼格式化
這篇文章主要介紹了詳解VS Code使用之Vue工程配置format代碼格式化,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
使用Vue與Firebase構(gòu)建實(shí)時(shí)聊天應(yīng)用的示例代碼
隨著互聯(lián)網(wǎng)通訊技術(shù)的不斷進(jìn)步,實(shí)時(shí)聊天應(yīng)用現(xiàn)在已成為我們?nèi)粘I钪胁豢苫蛉钡囊徊糠?無論是社交媒體平臺(tái)、工作溝通工具還是客戶支持系統(tǒng),實(shí)時(shí)聊天都在不斷被需求,今天,我們將介紹如何使用Vue.js與Firebase來構(gòu)建一個(gè)簡(jiǎn)單而強(qiáng)大的實(shí)時(shí)聊天應(yīng)用,需要的朋友可以參考下2024-11-11
詳解如何實(shí)現(xiàn)Element樹形控件Tree在懶加載模式下的動(dòng)態(tài)更新
這篇文章主要介紹了詳解如何實(shí)現(xiàn)Element樹形控件Tree在懶加載模式下的動(dòng)態(tài)更新,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-04-04
Vue2.0結(jié)合webuploader實(shí)現(xiàn)文件分片上傳功能
這篇文章主要介紹了Vue2.0結(jié)合webuploader實(shí)現(xiàn)文件分片上傳功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-03-03

