vuex的核心概念和基本使用詳解
介紹
Vuex是實現(xiàn)組件全局狀態(tài)(數(shù)據(jù))管理的一種機制,可以方便的實現(xiàn)組件之間的數(shù)據(jù)共享
開始
安裝
①直接下載方式
創(chuàng)建一個 vuex.js 文件 將https://unpkg.com/vuex這個網(wǎng)址里的內(nèi)容放到該文件夾里。
②CND方式
<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.auto.js"></script>
③NPM方式
npm install vuex --save
④Yarn方式
yarn add vuex
NPM方式安裝的使用方式
1.在 scr 文件里創(chuàng)建一個 store / index.js 的文件夾,寫入以下內(nèi)容。
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {},
mutations: {},
actions: {},
modules: {}
})
2.在main.js 里引入,然后掛載到 Vue 實例里
import Vue from 'vue'
import store from './store'
new Vue({
render: h => h(App),
store
}).$mount('#app')
store概念及使用
概念:
就是組件之間共享數(shù)據(jù)的。
只有 mutations 才能修改 store 中的數(shù)據(jù)
使用:
先定義后使用
定義
state: {
num: 0
}
使用
方式1(推薦)
<div>{{ numAlias }}</div>
import { mapState } from 'vuex'
export default {
//計算函數(shù)
computed: mapState({
// 傳字符串參數(shù) 'count' 等同于 `state => state.count`
numAlias: 'num',//常用key是自己起的名隨便 value接收的數(shù)據(jù)
// 箭頭函數(shù)可使代碼更簡練
count: state => state.count,
// 為了能夠使用 `this` 獲取局部狀態(tài),必須使用常規(guī)函數(shù)
countPlusLocalState (state) {
return state.count + this.localCount
}
//可以定義其余的計算函數(shù)
}),
//或者這樣
//計算函數(shù)
computed: {
mapState(['count'])
}
}
方式2
<div>{{ $store.state.count }}</div>
mutations概念及使用
概念:
修改store里的數(shù)據(jù),嚴格規(guī)定不能在其余的地方修改store的數(shù)據(jù),mutations里不要執(zhí)行異步操作。
mutation 必須同步執(zhí)行,不能異步執(zhí)行。
使用:
先定義方法后使用
定義
mutations: {
//increment自定義方法 store參數(shù)是store數(shù)據(jù), parameter參數(shù)是接收到的數(shù)據(jù),可不要
increment (state, parameter) {
// 變更狀態(tài)
state.num++
}
}
使用
方式1(推薦使用)
import { mapState, mapMutations } from 'vuex'
//方法
methods: {
...mapMutations([
// mutations自定義的方法名
'increment'
]),
love() {
// 直接this調(diào)用 this.increment('需要傳過去的數(shù)據(jù),可不要')
this.increment('Bin')
}
}
方式2
methods: {
love() {
// this.$store.commit('自定義的名稱', '傳過去的數(shù)據(jù),可不傳')
this.$store.commit('increment', 'data')
}
}
action概念及使用
概念:
用于處理異步操作。
如果通過異步操作變更數(shù)據(jù),必須通過action,而不能使用mutation,但是在action中還是要通過觸發(fā)mutation的方式間接變更數(shù)據(jù)。
Action 類似于 mutation,不同在于:
- Action 提交的是 mutation,而不是直接變更數(shù)據(jù)(狀態(tài))。
- Action 可以包含任意異步操作。
定義
mutations: {
//increment自定義方法 store參數(shù)是store數(shù)據(jù), parameter參數(shù)是接收到的數(shù)據(jù),可不要
increment (state, parameter) {
// 變更狀態(tài)
state.num++
}
},
actions: {
//add 自定義方法 context是參數(shù),可以把它當作vuex的實例
add(context) {
//可以通過context.commit('mutations中需要調(diào)用的方法')
context.commit('increment')
}
}
使用
方式1(推薦)
import { mapState, mapMutations, mapActions } from 'vuex'
export default {
methods: {
...mapActions([
'add', // 將 `this.add()` 映射為 `this.$store.dispatch('add')`
// `mapActions` 也支持載荷:
'add' // 將 `this.add(amount)` 映射為 `this.$store.dispatch('add', amount)`
]),
...mapActions({
add: 'add' // 將 `this.add()` 映射為 `this.$store.dispatch('increment')`
}),
love() {
// 直接this調(diào)用 this.add('需要傳過去的數(shù)據(jù),可不要')
this.add(data)
}
}
}
方式2
methods: {
love() {
// this.$store.dispatch('自定義的名稱', '傳過去的數(shù)據(jù),可不傳')
this.$store.dispatch('add', data)
}
}
getters概念及使用
概念:
getter用于對store中的數(shù)據(jù)進行加工處理形成新的數(shù)據(jù)。getting可以對store中已有的數(shù)據(jù)加工處理之后形成新的數(shù)據(jù),類似Vue的計算縮寫。
定義
state: {
num: 0
},
getters: {
doneTodos: state => {
return state.num = 10
}
}
使用
方式1(推薦)
<div>{{ doneTodos }}</div>
import { mapState, mapMutations, mapActions, mapGetters } from 'vuex'
export default {
//計算函數(shù)
computed: {
...mapState(['count']),
...mapmapGetters(['doneTodos'])
}
}
方式2
<div>{{ $store.getters.doneTodos }}</div>
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
vue如何使用element ui表格el-table-column在里面做判斷
這篇文章主要介紹了vue如何使用element ui表格el-table-column在里面做判斷問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
intellij?idea+vue前端調(diào)試配置圖文教程
在Vue項目開發(fā)過程中,當遇到應(yīng)用邏輯出現(xiàn)錯誤,但又無法準確定位的時候,知曉Vue項目調(diào)試技巧至關(guān)重要,debug是必備技能,這篇文章主要給大家介紹了關(guān)于intellij?idea+vue前端調(diào)試配置的相關(guān)資料,需要的朋友可以參考下2024-09-09
Vue?echarts實例項目商家銷量統(tǒng)計圖實現(xiàn)詳解
Echarts,它是一個與框架無關(guān)的?JS?圖表庫,但是它基于Js,這樣很多框架都能使用它,例如Vue,估計IONIC也能用,因為我的習(xí)慣,每次新嘗試做一個功能的時候,總要新創(chuàng)建個小項目,做做Demo2022-09-09
vue定時器清除不掉,導(dǎo)致功能頻繁執(zhí)行問題
這篇文章主要介紹了vue定時器清除不掉,導(dǎo)致功能頻繁執(zhí)行問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
Vue+axios實現(xiàn)統(tǒng)一接口管理的方法
這篇文章主要介紹了Vue+axios實現(xiàn)統(tǒng)一接口管理的方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-07-07
Vue2中Element?DatePicker組件設(shè)置默認日期及控制日期范圍
后臺項目想使用時間選擇器選擇一段時間進行數(shù)據(jù)篩選,所以下面這篇文章主要給大家介紹了關(guān)于Vue2中Element?DatePicker組件設(shè)置默認日期及控制日期范圍的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-11-11

