Vuex的使用

Vuex 超詳細使用教程(從入門到精通)

一、Vuex 是什么?

Vuex 是專門為 Vue.js 設計的狀態管理庫,它采用集中式存儲管理應用的所有組件的狀態。簡單來說,Vuex 就是一個"全局變量倉庫",所有組件都可以從這里獲取數據或修改數據。

二、為什么要用 Vuex?

當你的 Vue 應用變得復雜時,組件之間的數據共享和通信會變得困難。Vuex 解決了以下問題:

  1. 多個組件共享同一狀態

  2. 不同組件需要變更同一狀態

  3. 組件深層嵌套時的數據傳遞

三、安裝與基礎配置

1. 安裝 Vuex

npm install vuex --save
# 或
yarn add vuex

2. 創建 Store

在項目中創建?store/index.js?文件:

import Vue from 'vue'
import Vuex from 'vuex'Vue.use(Vuex) // 告訴 Vue 使用 Vuex// 創建 Vuex Store 實例
const store = new Vuex.Store({// 在這里配置 Vuex
})export default store

3. 在 Vue 實例中引入

在?main.js?中:

import Vue from 'vue'
import App from './App.vue'
import store from './store' // 導入我們創建的 storenew Vue({store, // 注入 storerender: h => h(App)
}).$mount('#app')

四、核心概念詳解

1. State - 狀態倉庫

State 就是存儲數據的地方,相當于組件中的 data。

const store = new Vuex.Store({state: {count: 0,user: {name: '張三',age: 25},todos: [{ id: 1, text: '學習Vuex', done: true },{ id: 2, text: '寫項目', done: false }]}
})
在組件中訪問 State

方法1:直接通過?this.$store.state?訪問

computed: {count() {return this.$store.state.count},userName() {return this.$store.state.user.name}
}

方法2:使用?mapState?輔助函數

import { mapState } from 'vuex'export default {computed: {// 數組形式 - 同名映射...mapState(['count', 'user']),// 對象形式 - 可自定義名稱...mapState({myCount: 'count',currentUser: state => state.user})}
}

2. Getters - 計算屬性

Getters 相當于 Store 的計算屬性,用于從 state 中派生出一些狀態。

const store = new Vuex.Store({state: {todos: [{ id: 1, text: '學習Vuex', done: true },{ id: 2, text: '寫項目', done: false }]},getters: {// 獲取已完成的任務doneTodos: state => {return state.todos.filter(todo => todo.done)},// 獲取未完成的任務數量undoneTodosCount: (state, getters) => {return state.todos.length - getters.doneTodos.length},// 通過ID獲取特定任務getTodoById: state => id => {return state.todos.find(todo => todo.id === id)}}
})
在組件中使用 Getters

方法1:直接通過?this.$store.getters?訪問

computed: {doneTodos() {return this.$store.getters.doneTodos},todo() {return this.$store.getters.getTodoById(2)}
}

方法2:使用?mapGetters?輔助函數

import { mapGetters } from 'vuex'export default {computed: {// 數組形式 - 同名映射...mapGetters(['doneTodos', 'undoneTodosCount']),// 對象形式 - 可自定義名稱...mapGetters({doneList: 'doneTodos',todo: 'getTodoById'})},methods: {getTodo(id) {return this.todo(id) // 使用帶參數的getter}}
}

3. Mutations - 修改狀態

Mutations 是修改 State 的唯一途徑,每個 mutation 都有一個字符串的?事件類型 (type)?和一個?回調函數 (handler)

const store = new Vuex.Store({state: {count: 1},mutations: {// 基本形式increment(state) {state.count++},// 帶參數的形式incrementBy(state, payload) {state.count += payload.amount},// 對象風格的提交decrement(state, { amount }) {state.count -= amount}}
})
提交 Mutations

方法1:直接提交

methods: {increment() {this.$store.commit('increment')},addFive() {// 提交帶參數的mutationthis.$store.commit('incrementBy', { amount: 5 })// 對象風格提交this.$store.commit({type: 'incrementBy',amount: 5})}
}

方法2:使用?mapMutations?輔助函數

methods: {increment() {this.$store.commit('increment')},addFive() {// 提交帶參數的mutationthis.$store.commit('incrementBy', { amount: 5 })// 對象風格提交this.$store.commit({type: 'incrementBy',amount: 5})}
}
import { mapMutations } from 'vuex'export default {methods: {// 數組形式 - 同名映射...mapMutations(['increment', 'incrementBy']),// 對象形式 - 可自定義名稱...mapMutations({add: 'increment',addAmount: 'incrementBy'}),// 使用方法addFive() {this.addAmount({ amount: 5 })}}
}
重要規則:
  1. Mutation 必須是同步函數

  2. 不要在 mutation 中執行異步操作

  3. 建議使用常量替代 Mutation 事件類型(大型項目)

4. Actions - 處理異步操作

Actions 類似于 mutations,不同在于:

  • Actions 提交的是 mutations,而不是直接變更狀態

  • Actions 可以包含任意異步操作

const store = new Vuex.Store({state: {count: 0},mutations: {increment(state) {state.count++}},actions: {// 基本形式incrementAsync({ commit }) {setTimeout(() => {commit('increment')}, 1000)},// 帶參數的形式incrementByAsync({ commit }, payload) {return new Promise((resolve) => {setTimeout(() => {commit('increment')resolve()}, payload.delay)})},// 組合多個actionactionA({ commit }) {return new Promise((resolve) => {setTimeout(() => {commit('someMutation')resolve()}, 1000)})},actionB({ dispatch, commit }) {return dispatch('actionA').then(() => {commit('someOtherMutation')})}}
})
分發 Actions

方法1:直接分發

methods: {increment() {this.$store.dispatch('incrementAsync')},addLater() {// 帶參數this.$store.dispatch('incrementByAsync', { delay: 2000 })// 對象風格this.$store.dispatch({type: 'incrementByAsync',delay: 2000})// 處理Promisethis.$store.dispatch('actionB').then(() => {console.log('Action completed')})}
}

方法2:使用?mapActions?輔助函數

import { mapActions } from 'vuex'export default {methods: {// 數組形式 - 同名映射...mapActions(['incrementAsync', 'incrementByAsync']),// 對象形式 - 可自定義名稱...mapActions({add: 'incrementAsync',addLater: 'incrementByAsync'}),// 使用方法addFiveLater() {this.addLater({ delay: 5000 }).then(() => console.log('Done!'))}}
}

5. Modules - 模塊化

當應用變得復雜時,Store 對象可能變得臃腫。Vuex 允許我們將 store 分割成模塊(module)。

const moduleA = {namespaced: true, // 啟用命名空間state: { count: 0 },mutations: {increment(state) {state.count++}},getters: {doubleCount(state) {return state.count * 2}}
}const moduleB = {namespaced: true,state: { list: [] },actions: {fetchList({ commit }) {// 獲取數據...}}
}const store = new Vuex.Store({modules: {a: moduleA,b: moduleB}
})
訪問模塊內容
// 訪問模塊 state
this.$store.state.a.count // -> moduleA 的 state
this.$store.state.b.list  // -> moduleB 的 state// 提交模塊 mutation
this.$store.commit('a/increment')// 分發模塊 action
this.$store.dispatch('b/fetchList')// 使用模塊 getter
this.$store.getters['a/doubleCount']
使用輔助函數
import { mapState, mapGetters, mapMutations, mapActions } from 'vuex'export default {computed: {// 模塊 state...mapState('a', ['count']),...mapState('b', ['list']),// 模塊 getters...mapGetters('a', ['doubleCount'])},methods: {// 模塊 mutations...mapMutations('a', ['increment']),// 模塊 actions...mapActions('b', ['fetchList'])}
}
模塊局部狀態

在模塊內部的 mutation 和 getter 中,接收的第一個參數是模塊的局部狀態對象:

const moduleA = {state: { count: 0 },mutations: {increment(state) {// state 是模塊的局部狀態state.count++}},getters: {doubleCount(state) {return state.count * 2},// 可以訪問根節點狀態sumWithRootCount(state, getters, rootState) {return state.count + rootState.count}},actions: {// 在action中可以通過 context.rootState 訪問根狀態incrementIfOdd({ state, commit, rootState }) {if ((state.count + rootState.count) % 2 === 1) {commit('increment')}}}
}

五、實際項目應用示例

1. 用戶認證模塊示例

// store/modules/auth.js
const state = {user: null,token: localStorage.getItem('token') || '',status: '' // 'loading', 'success', 'error'
}const getters = {isAuthenticated: state => !!state.token,authStatus: state => state.status,currentUser: state => state.user
}const mutations = {AUTH_REQUEST(state) {state.status = 'loading'},AUTH_SUCCESS(state, { token, user }) {state.status = 'success'state.token = tokenstate.user = userlocalStorage.setItem('token', token)},AUTH_ERROR(state) {state.status = 'error'localStorage.removeItem('token')},LOGOUT(state) {state.user = nullstate.token = ''localStorage.removeItem('token')}
}const actions = {login({ commit }, userData) {return new Promise((resolve, reject) => {commit('AUTH_REQUEST')axios.post('/api/auth/login', userData).then(res => {const { token, user } = res.datacommit('AUTH_SUCCESS', { token, user })resolve(res)}).catch(err => {commit('AUTH_ERROR')localStorage.removeItem('token')reject(err)})})},logout({ commit }) {return new Promise(resolve => {commit('LOGOUT')resolve()})}
}export default {namespaced: true,state,getters,mutations,actions
}

2. 在組件中使用

寫文章-CSDN創作中心

<template><div><div v-if="!isAuthenticated"><button @click="login">登錄</button></div><div v-else>歡迎, {{ currentUser.name }}!<button @click="logout">退出</button></div></div>
</template><script>
import { mapGetters, mapActions } from 'vuex'export default {computed: {...mapGetters('auth', ['isAuthenticated', 'currentUser'])},methods: {...mapActions('auth', ['login', 'logout']),login() {const userData = { username: 'test', password: '123456' }this.login(userData).then(() => {this.$router.push('/dashboard')}).catch(error => {console.error('登錄失敗:', error)})}}
}</script>

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/pingmian/93682.shtml
繁體地址,請注明出處:http://hk.pswp.cn/pingmian/93682.shtml
英文地址,請注明出處:http://en.pswp.cn/pingmian/93682.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

pytorch 數據預處理,加載,訓練,可視化流程

流程定義自定義數據集類定義訓練和驗證的數據增強定義模型、損失函數和優化器訓練循環&#xff0c;包括驗證訓練可視化整個流程模型評估高級功能擴展混合精度訓練?分布式訓練?{:width“50%” height“50%”} 定義自定義數據集類 # #1. 自定義數據集類 # class CustomImageD…

Prompt工程:OCR+LLM文檔處理的精準制導系統

在PDF OCR與大模型結合的實際應用中&#xff0c;很多團隊會發現一個現象&#xff1a;同樣的OCR文本&#xff0c;不同的Prompt設計會產生截然不同的提取效果。有時候準確率能達到95%&#xff0c;有時候卻只有60%。這背后的關鍵就在于Prompt工程的精細化程度。 &#x1f3af; 為什…

RecSys:粗排模型和精排特征體系

粗排 在推薦系統鏈路中&#xff0c;排序階段至關重要&#xff0c;通常分為召回、粗排和精排三個環節。粗排作為精排前的預處理階段&#xff0c;需要在效果和性能之間取得平衡。 雙塔模型 后期融合&#xff1a;把用戶、物品特征分別輸入不同的神經網絡&#xff0c;不對用戶、…

spring聲明式事務,finally 中return對事務回滾的影響

finally 塊中使用 return 是一個常見的編程錯誤&#xff0c;它會&#xff1a; 跳過正常的事務提交流程。吞掉異常&#xff0c;使錯誤處理失效 導致不可預測的事務行為Java 中 finally 和 return 的執行機制&#xff1a;1. finally 塊的基本特性 在 Java 中&#xff0c;finally …

WPF 打印報告圖片大小的自適應(含完整示例與詳解)

目標&#xff1a;在 FlowDocument 報告里&#xff0c;根據 1~6 張圖片的數量&#xff0c; 自動選擇 2 行 3 列 的最佳布局&#xff1b;在只有 1、2、4 張時保持“占滿感”&#xff0c;打印清晰且不變形。規則一覽&#xff1a;1 張 → 占滿 23&#xff08;大圖居中&#xff09;…

【AI大模型前沿】百度飛槳PaddleOCR 3.0開源發布,支持多語言、手寫體識別,賦能智能文檔處理

系列篇章&#x1f4a5; No.文章1【AI大模型前沿】深度剖析瑞智病理大模型 RuiPath&#xff1a;如何革新癌癥病理診斷技術2【AI大模型前沿】清華大學 CLAMP-3&#xff1a;多模態技術引領音樂檢索新潮流3【AI大模型前沿】浙大攜手阿里推出HealthGPT&#xff1a;醫學視覺語言大模…

迅為RK3588開發板Android12 制作使用系統簽名

在 Android 源碼 build/make/target/product/security/下存放著簽名文件&#xff0c;如下所示&#xff1a;將北京迅為提供的 keytool 工具拷貝到 ubuntu 中&#xff0c;然后將 Android11 或 Android12 源碼build/make/target/product/security/下的 platform.pk8 platform.x509…

Day08 Go語言學習

1.安裝Go和Goland 2.新建demo項目實踐語法并使用git實踐版本控制操作 2.1 Goland配置 路徑**&#xff1a;** GOPATH workspace GOROOT golang 文件夾&#xff1a; bin 編譯后的可執行文件 pkg 編譯后的包文件 src 源文件 遇到問題1&#xff1a;運行 ‘go build awesomeProject…

Linux-文件創建拷貝刪除剪切

文章目錄Linux文件相關命令ls通配符含義touch 創建文件命令示例cp 拷貝文件rm 刪除文件mv剪切文件Linux文件相關命令 ls ls是英文單詞list的簡寫&#xff0c;其功能為列出目錄的內容&#xff0c;是用戶最常用的命令之一&#xff0c;它類似于DOS下的dir命令。 Linux文件或者目…

RabbitMQ:交換機(Exchange)

目錄一、概述二、Direct Exchange &#xff08;直連型交換機&#xff09;三、Fanout Exchange&#xff08;扇型交換機&#xff09;四、Topic Exchange&#xff08;主題交換機&#xff09;五、Header Exchange&#xff08;頭交換機&#xff09;六、Default Exchange&#xff08;…

【實時Linux實戰系列】基于實時Linux的物聯網系統設計

隨著物聯網&#xff08;IoT&#xff09;技術的飛速發展&#xff0c;越來越多的設備被連接到互聯網&#xff0c;形成了一個龐大而復雜的網絡。這些設備從簡單的傳感器到復雜的工業控制系統&#xff0c;都在實時地產生和交換數據。實時Linux作為一種強大的操作系統&#xff0c;為…

第五天~提取Arxml中描述信息New_CanCluster--Expert

?? ARXML描述信息提取:挖掘汽車電子設計的"知識寶藏" 在AUTOSAR工程中,描述信息如同埋藏在ARXML文件中的金礦,而New_CanCluster--Expert正是打開這座寶藏的密鑰。本文將帶您深度探索ARXML描述信息的提取藝術,解鎖汽車電子設計的核心知識資產! ?? 為什么描述…

開源 C++ QT Widget 開發(一)工程文件結構

文章的目的為了記錄使用C 進行QT Widget 開發學習的經歷。臨時學習&#xff0c;完成app的開發。開發流程和要點有些記憶模糊&#xff0c;趕緊記錄&#xff0c;防止忘記。 相關鏈接&#xff1a; 開源 C QT Widget 開發&#xff08;一&#xff09;工程文件結構-CSDN博客 開源 C…

手寫C++ string類實現詳解

類定義cppnamespace ym {class string {private:char* _str; // 字符串數據size_t _size; // 當前字符串長度size_t _capacity; // 當前分配的內存容量static const size_t npos -1; // 特殊值&#xff0c;表示最大可能位置public:// 構造函數和析構函數string(…

C++信息學奧賽一本通-第一部分-基礎一-第3章-第2節

C信息學奧賽一本通-第一部分-基礎一-第3章-第2節 2057 星期幾 #include <iostream>using namespace std;int main() {int day; cin >> day;switch (day) {case 1:cout << "Monday";break;case 2:cout << "Tuesday";break;case 3:c…

【leetcode 3】最長連續序列 (Longest Consecutive Sequence) - 解題思路 + Golang實現

最長連續序列 (Longest Consecutive Sequence) - LeetCode 題解 題目描述 給定一個未排序的整數數組 nums&#xff0c;找出數字連續的最長序列&#xff08;不要求序列元素在原數組中連續&#xff09;的長度。要求設計并實現時間復雜度為 O(n) 的算法解決此問題。 示例 1&#x…

礦物分類系統開發筆記(一):數據預處理

目錄 一、數據基礎與預處理目標 二、具體預處理步驟及代碼解析 2.1 數據加載與初步清洗 2.2 標簽編碼 2.3 缺失值處理 &#xff08;1&#xff09;刪除含缺失值的樣本 &#xff08;2&#xff09;按類別均值填充 &#xff08;3&#xff09;按類別中位數填充 &#xff08;…

《UE5_C++多人TPS完整教程》學習筆記43 ——《P44 奔跑混合空間(Running Blending Space)》

本文為B站系列教學視頻 《UE5_C多人TPS完整教程》 —— 《P44 奔跑混合空間&#xff08;Running Blending Space&#xff09;》 的學習筆記&#xff0c;該系列教學視頻為計算機工程師、程序員、游戲開發者、作家&#xff08;Engineer, Programmer, Game Developer, Author&…

TensorRT-LLM.V1.1.0rc1:Dockerfile.multi文件解讀

一、TensorRT-LLM有三種安裝方式&#xff0c;從簡單到難 1.NGC上的預構建發布容器進行部署,見《tensorrt-llm0.20.0離線部署DeepSeek-R1-Distill-Qwen-32B》。 2.通過pip進行部署。 3.從源頭構建再部署&#xff0c;《TensorRT-LLM.V1.1.0rc0:在無 GitHub 訪問權限的服務器上編…

UniApp 實現pdf上傳和預覽

一、上傳1、html<template><button click"takeFile">pdf上傳</button> </template>2、JStakeFile() {// #ifdef H5// H5端使用input方式選擇文件const input document.createElement(input);input.type file;input.accept .pdf;input.onc…