Vuex 入門教程
Vuex 是什么?
官方給出的解釋:Vuex 是一個(gè)專為 Vue.js 應(yīng)用程序開發(fā)的狀態(tài)管理模式。它采用集中式存儲管理應(yīng)用的所有組件的狀態(tài),并以相應(yīng)的規(guī)則保證狀態(tài)以一種可預(yù)測的方式發(fā)生變化。
相信很多新選手看完這段話有種絕望的感覺。開始我也是這樣的,后來我想到了一個(gè)比方!
比如某年級有5個(gè)小班,每個(gè)小班有25個(gè)同學(xué),但是只有一個(gè)老師授課,假如5個(gè)小班就對應(yīng)著5個(gè)組件,每個(gè)班的25個(gè)同學(xué)就相當(dāng)于每個(gè)組件中的25條數(shù)據(jù),這個(gè)老師就相當(dāng)于 vuex ,老師講的課就相當(dāng)于每一條數(shù)據(jù)。要保證每個(gè)同學(xué)受到同樣的教育,就需要這個(gè)老師把每節(jié)課分別講5遍,還不能保證每個(gè)班的同學(xué)聽到的效果相同。一段時(shí)間后,老師覺得這樣特別麻煩還很累,就想了一個(gè)辦法,找了一個(gè)大教室,把這5個(gè)小班的同學(xué)合并到一起,這樣每個(gè)課程只需要講一次就好啦,而且還保證了每個(gè)班的同學(xué)聽到的效果相同。這就是 vuex 的作用,把各個(gè)組件中用到的數(shù)據(jù)統(tǒng)一管理,同步發(fā)放,省時(shí)省心省力。
那這個(gè) vuex 怎么用呢?讓我們從一個(gè)簡單的 Vue 計(jì)數(shù)應(yīng)用開始
一、基本用法
1. 初始化并創(chuàng)建一個(gè)項(xiàng)目
vue init webpack-simple vuex-demo cd vuex-demo npm install
2. 安裝 vuex
npm install vuex -S
3. 在 src 目錄下創(chuàng)建 store.js 文件,并在 main.js 文件中導(dǎo)入并配置
store.js 中寫入
import Vue from 'vue' //引入 vuex 并 use import Vuex from 'vuex' Vue.use(Vuex)
main.js 文件
import Vue from 'vue'
import App from './App.vue'
import store from './assets/store' //導(dǎo)入 store 對象
new Vue({
//配置 store 選項(xiàng),指定為 store 對象,會自動(dòng)將 store 對象注入到所有子組件中,在子組件中通過 this.$store 訪問該 store 對象
store,
el: '#app',
render: h => h(App)
})
4. 編輯 store.js 文件
在應(yīng)用 vuex 之前,我們還是需要看懂這個(gè)流程圖,其實(shí)很簡單。

vuex
① Vue Components 是我們的 vue 組件,組件會觸發(fā)(dispatch)一些事件或動(dòng)作,也就是圖中的 Actions;
② 我們在組件中發(fā)出的動(dòng)作,肯定是想獲取或者改變數(shù)據(jù)的,但是在 vuex 中,數(shù)據(jù)是集中管理的,我們不能直接去更改數(shù)據(jù),所以會把這個(gè)動(dòng)作提交(Commit)到 Mutations 中;
③ 然后 Mutations 就去改變(Mutate)State 中的數(shù)據(jù);
④ 當(dāng) State 中的數(shù)據(jù)被改變之后,就會重新渲染(Render)到 Vue Components 中去,組件展示更新后的數(shù)據(jù),完成一個(gè)流程。
Vuex 的核心是 Store(倉庫),相當(dāng)于是一個(gè)容器,一個(gè) Store 實(shí)例中包含以下屬性的方法:
state 定義屬性(狀態(tài) 、數(shù)據(jù))
store.js 中寫入
// 定義屬性(數(shù)據(jù))
var state = {
count:6
}
// 創(chuàng)建 store 對象
const store = new Vuex.Store({
state
})
// 導(dǎo)出 store 對象
export default store;
方式1、 在 app.vue 中就能通過 this.$store 訪問該 store 對象 ,獲取該 count 。
<template>
<div id="app">
//把 count 方法直接寫入,可自己執(zhí)行
<h1>{{count}}</h1>
</div>
</template>
<script>
export default {
name: 'app',
computed:{
count(){
//返回獲取到的數(shù)據(jù)
return this.$store.state.count
}
}
}
</script>
方式2、vuex 提供的 mapGetters 和 mapActions 來訪問
mapGetters 用來獲取屬性(數(shù)據(jù))
① 在 app.vue 中引入 mapGetters
import {mapGetters} from 'vuex'
② 在 app.vue 文件的計(jì)算屬性中調(diào)用 mapGetters 輔助方法,并傳入一個(gè)數(shù)組,在數(shù)組中指定要獲取的屬性 count
<script>
import {mapGetters,mapActions} from 'vuex'
export default {
name: 'app',
computed:mapGetters([
//此處的 count 與以下 store.js 文件中 getters 內(nèi)的 count 相對應(yīng)
'count'
])
}
</script>
③ 在 store.js 中定義 getters 方法并導(dǎo)出
getters 用來獲取屬性
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
// 定義屬性(數(shù)據(jù))
var state = {
count:6
}
// 定義 getters
var getters={
//需要傳個(gè)形參,用來獲取 state 屬性
count(state){
return state.count
}
}
// 創(chuàng)建 store 對象
const store = new Vuex.Store({
state,
getters
})
// 導(dǎo)出 store 對象
export default store;
這樣頁面上就會顯示傳過來的數(shù)據(jù)了!接下來我們來通過動(dòng)作改變獲取到的數(shù)據(jù)
④在 store.js 中定義 actions 和 mutations 方法并導(dǎo)出
actions 定義方法(動(dòng)作)
commit 提交變化,修改數(shù)據(jù)的唯一方式就是顯示的提交 mutations
mutations 定義變化,處理狀態(tài)(數(shù)據(jù))的改變
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
// 定義屬性(數(shù)據(jù))
var state = {
count:6
}
// 定義 getters
var getters={
count(state){
return state.count
}
}
// 定義 actions ,要執(zhí)行的動(dòng)作,如流程的判斷、異步請求
const actions ={
// ({commit,state}) 這種寫法是 es6 中的對象解構(gòu)
increment({commit,state}){
//提交一個(gè)名為 increment 的變化,名字可自定義,可以認(rèn)為是類型名,與下方 mutations 中的 increment 對應(yīng)
//commit 提交變化,修改數(shù)據(jù)的唯一方式就是顯式的提交 mutations
commit('increment')
}
}
// 定義 mutations ,處理狀態(tài)(數(shù)據(jù)) 的改變
const mutations ={
//與上方 commit 中的 ‘increment' 相對應(yīng)
increment(state){
state.count ++;
}
}
// 創(chuàng)建 store 對象
const store = new Vuex.Store({
state,
getters,
actions,
mutations
})
// 導(dǎo)出 store 對象
export default store;
⑤ 在 app.vue 中引入 mapActions ,并調(diào)用
mapActions 用來獲取方法(動(dòng)作)
import {mapGetters,mapActions} from 'vuex'
調(diào)用 mapActions 輔助方法,并傳入一個(gè)數(shù)組,在數(shù)組中指定要獲取的方法 increment
<template>
<div id="app">
//這個(gè) increment 方法與下面 methods 中的 increment 相對應(yīng)
<button @click="increment">增加</button>
<button>減少</button>
<h1>{{count}}</h1>
</div>
</template>
<script>
import {mapGetters,mapActions} from 'vuex'
export default {
name: 'app',
computed:mapGetters([
'count'
]),
methods:mapActions([
//該 increment 來自 store.js 中導(dǎo)出的 actions 和 mutations 中的 increment
'increment',
])
}
</script>
這樣就能通過 button 來改變獲取到的 count 了。
看起來確實(shí)是挺繞的,需要在理解了原理的情況下,再細(xì)細(xì)琢磨,加深理解。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue路由跳轉(zhuǎn)傳遞參數(shù)的方式總結(jié)
在本篇文章和里小編給各位總結(jié)了關(guān)于vue路由跳轉(zhuǎn)傳遞參數(shù)的三種方式以及相關(guān)代碼,需要的朋友們可以學(xué)習(xí)下。2020-05-05
vue3(ts)類型EventTarget上不存在屬性value的問題
這篇文章主要介紹了vue3(ts)類型EventTarget上不存在屬性value的問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
ant?菜單組件報(bào)錯(cuò)Cannot?read?property?‘isRootMenu‘?of?undefin
這篇文章主要介紹了ant?菜單組件報(bào)錯(cuò)Cannot?read?property?‘isRootMenu‘?of?undefined解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
vue3在單個(gè)組件中實(shí)現(xiàn)類似mixin的事件調(diào)用
這篇文章主要為大家詳細(xì)介紹了vue3如何在單個(gè)組件中實(shí)現(xiàn)類似mixin的事件調(diào)用,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01
Vue通過axios發(fā)送ajax請求基礎(chǔ)演示
這篇文章主要介紹了Vue通過axios發(fā)送ajax請求基礎(chǔ)演示,包括了axios發(fā)送簡單get請求,axios get傳參,axios發(fā)送post請求等基礎(chǔ)代碼演示需要的朋友可以參考下2023-02-02
解決vue中axios設(shè)置超時(shí)(超過5分鐘)沒反應(yīng)的問題
這篇文章主要介紹了解決vue中axios設(shè)置超時(shí)(超過5分鐘)沒反應(yīng)的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
vue3中實(shí)現(xiàn)雙向數(shù)據(jù)綁定的方法
Vue3中,雙向數(shù)據(jù)綁定主要通過v-model指令實(shí)現(xiàn),v-model是一個(gè)語法糖,結(jié)合了v-bind和v-on指令來實(shí)現(xiàn)數(shù)據(jù)的雙向綁定,它在內(nèi)部做了綁定數(shù)據(jù)和監(jiān)聽輸入事件兩件事,感興趣的朋友跟隨小編一起看看吧2024-12-12
element-ui 上傳圖片后清空圖片顯示的實(shí)例
今天小編就為大家分享一篇element-ui 上傳圖片后清空圖片顯示的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
Vue.js實(shí)現(xiàn)一個(gè)SPA登錄頁面的過程【推薦】
本篇文章主要介紹了Vue.js寫一個(gè)SPA登錄頁面過程的相關(guān)知識,具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-04-04

