vue全家桶-vuex深入講解
使用
index.js
import Vue from 'vue' import Vuex from 'vuex' import getters from './getters' import app from './modules/app' import settings from './modules/settings' import user from './modules/user' import system from './modules/system' Vue.use(Vuex) const store = new Vuex.Store({ modules: { app, settings, user, system }, getters }) export default store
getters.js
const getters = { sidebar: state => state.app.sidebar, device: state => state.app.device, token: state => state.user.token, avatar: state => state.user.avatar, name: state => state.user.name, currentuserinfo: state => state.system.currentuserinfo, count: state => state.system.count, } export default getters
system.js
const system = { namespaced: true, state: { currentuserinfo: {}, count: 0, }, mutations: { SET_CURRENTUSERINFO: (state, currentuserinfo) => { state.currentuserinfo = currentuserinfo }, SET_COUNT: (state, count) => { state.count = count }, } } export default system
全局使用:main.js文件中
import store from './store' new Vue({ el: '#app', router, store, render: h => h(App) })
Vuex概述
Vuex是實現(xiàn)組件全局狀態(tài)(數(shù)據(jù))管理的一種機制,可以方便的實現(xiàn)組件之間的數(shù)據(jù)共享
使用Vuex管理數(shù)據(jù)的好處:
A.能夠在vuex中集中管理共享的數(shù)據(jù),便于開發(fā)和后期進行維護
B.能夠高效的實現(xiàn)組件之間的數(shù)據(jù)共享,提高開發(fā)效率
C.存儲在vuex中的數(shù)據(jù)是響應式的,當數(shù)據(jù)發(fā)生改變時,頁面中的數(shù)據(jù)也會同步更新
Vuex中的核心特性
vuex中的主要核心概念如下:
- State
- Mutation
- Action
- Getter
A.State
State提供唯一的公共數(shù)據(jù)源,所有共享的數(shù)據(jù)都要統(tǒng)一放到Store中的State中存儲 例如,打開項目中的store.js文件,在State對象中可以添加我們要共享的數(shù)據(jù),如:count:0
在組件中訪問State的方式: 1).this.$store.state.全局數(shù)據(jù)名稱 如:this.$store.state.count 2).先按需導入mapState函數(shù): import { mapState } from 'vuex' 然后數(shù)據(jù)映射為計算屬性: computed:{ ...mapState(['全局數(shù)據(jù)名稱']) }
this.$store.state.全局數(shù)據(jù)名稱-組件訪問State中的數(shù)據(jù)的第一種方式
//訪問 console.log("1111",this.$store.state.system.count); <h3>當前最新的count值為:{{$store.state.system.count}}</h3>
組件訪問State中的數(shù)據(jù)的第二種方式:按需導入
2).先按需導入mapState函數(shù): import { mapState } from 'vuex' //將全局數(shù)據(jù),用展開運算符映射為當前組件的計算屬性 // 然后數(shù)據(jù)映射為計算屬性: computed:{ ...mapState(['count']) } mapState()可以傳入對象或者數(shù)組 傳入數(shù)組用法: mapState(['counte', 'name','age']) // 傳入對象用法:可以重命名store中的數(shù)據(jù) ...mapState({ sCounter: state => state.name, ...... }) computed:{ ...mapState({ count: state => state.system.count, ...... }), }
B.Mutation
Mutation用于修改變更$store中的數(shù)據(jù)
只能通過Mutation變更Store數(shù)據(jù),不可以直接操作Store中的數(shù)據(jù)通過這種方式雖然操作起來稍微繁瑣點,但是可以集中監(jiān)控所有的數(shù)據(jù)變化
this.$store.commit是觸發(fā)Mutation的第一種方式
1.定義:
const system = { namespaced: true, state: { count: 0, }, mutations: { add(state) { //變更狀態(tài) state.count++ } } } export default system
2.使用
<template> <div> <h3>當前最新的count值為:{{$store.state.system.count}}</h3> <el-button type="primary" @click="btnHandler1">+1</el-button> </div> </template> <script> export default { name: 'Addition', props: { }, data() { return { } }, computed: {}, mounted() {}, methods: { btnHandler1() { this.$store.commit("system/add") }, } } </script> <style scoped> </style>
1.傳參—定義
mutations: { add(state) { state.count++ }, addN(state, step) { state.count += step } }
2.傳參-使用
methods: { btnHandler1() { this.$store.commit("system/add") }, btnHandler2(val){ // commit 的作用就是調(diào)用某個mutation函數(shù) this.$store.commit("system/addN",val) }, }
觸發(fā)Mutation的第二種方式,按需導入
從vuex中按需導入mapMutations 函數(shù)
import { mapMutations } from 'vuex'
通過剛才導入的mapMutations 函數(shù),將需要的mapMutations 函數(shù),映射為當前組件的methods方法:
sub(state) { state.count-- }, subN(state, step) { state.count -= step },
method:{ ...mapMutations({ sub: 'system/sub' }), btnHandler1(){ this.sub()//直接引用 }, btnHandler2(val){ this.subN(val) }, }
C.Action
Action用于處理異步任務
如果通過異步操作變更數(shù)據(jù),必須通過Action,而不能使用Mutation,但Action中還是要通過出發(fā)Mutation的方式間接變更數(shù)據(jù)
this.$store.dispatch()是觸發(fā)actions的第一種方式
actions: { addAsync(content) { setTimeout(() => { // 在actions中,不能直接修改state中的數(shù)據(jù) // 必須通過content.commit() 去觸發(fā)某個mutations才行 content.commit('add') }, 1000) } }
methods: { // 異步的讓count自增+1 btnHandler3(){ // 這里的dispatch函數(shù),專門用來觸發(fā)actions this.$store.dispatch('system/addAsync') }, }
actions攜帶參數(shù)
觸發(fā)actions異步任務時攜帶參數(shù)
actions: { addNAsync(content, step) { setTimeout(() => { // 在actions中,不能直接修改state中的數(shù)據(jù) // 必須通過content.commit() 去觸發(fā)某個mutations才行 content.commit('addN', step) }, 1000) }, }
methods: { btnHandler4(){ // 這里的dispatch函數(shù),專門用來觸發(fā)actions,傳參 this.$store.dispatch('system/addNAsync',3) }, }
觸發(fā)actions的第二種方式:按需導入
actions: { subAsync(content) { setTimeout(() => { // 在actions中,不能直接修改state中的數(shù)據(jù) // 必須通過content.commit() 去觸發(fā)某個mutations才行 content.commit('sub') }, 1000) }, subNAsync(content, step) { setTimeout(() => { // 在actions中,不能直接修改state中的數(shù)據(jù) // 必須通過content.commit() 去觸發(fā)某個mutations才行 content.commit('subN', step) }, 1000) }, }
import {mapActions } from 'vuex' methods:{ ...mapActions({ subAsync: 'system/subAsync', subNAsync: 'system/subNAsync', }), btnHandler3(){ this.subAsync() }, btnHandler4(){ this.subNAsync(3) }, }
D.Getter
Getter用于對Store中的數(shù)據(jù)進行加工處理形成新的數(shù)據(jù)
它只會包裝Store中保存的數(shù)據(jù),并不會修改Store中保存的數(shù)據(jù),當Store中的數(shù)據(jù)發(fā)生變化時,Getter生成的內(nèi)容也會隨之變化
打開store.js文件,添加getters,如下:
使用getters的第一種方式
//system.js文件中的 getters中的showNum <h3>{{$store.getters['system/showNum']}}</h3> console.log('$store.state',this.$store.getters['system/showNum']);
使用getters的第二種方式
<h3>{{showNum}}</h3>
computed: { ...mapGetters({ showNum: 'system/showNum', }) },
代碼總結
system.js
const system = { namespaced: true, state: { currentuserinfo: {}, count: 0, }, // 只有mutations中定義的函數(shù),才有全力修改state中的數(shù)據(jù) mutations: { // SET_CURRENTUSERINFO: (state, currentuserinfo) => { // state.currentuserinfo = currentuserinfo // }, // SET_COUNT: (state, count) => { // state.count = count // }, add(state) { state.count++ }, addN(state, step) { state.count += step }, sub(state) { state.count-- }, subN(state, step) { state.count -= step }, }, actions: { addAsync(content) { setTimeout(() => { // 在actions中,不能直接修改state中的數(shù)據(jù) // 必須通過content.commit() 去觸發(fā)某個mutations才行 content.commit('add') }, 1000) }, addNAsync(content, step) { setTimeout(() => { // 在actions中,不能直接修改state中的數(shù)據(jù) // 必須通過content.commit() 去觸發(fā)某個mutations才行 content.commit('addN', step) }, 1000) }, subAsync(content) { setTimeout(() => { // 在actions中,不能直接修改state中的數(shù)據(jù) // 必須通過content.commit() 去觸發(fā)某個mutations才行 content.commit('sub') }, 1000) }, subNAsync(content, step) { setTimeout(() => { // 在actions中,不能直接修改state中的數(shù)據(jù) // 必須通過content.commit() 去觸發(fā)某個mutations才行 content.commit('subN', step) }, 1000) }, }, getters: { //添加了一個showNum的屬性 showNum(state) { return '最新的count值為:【' + state.count + '】'; } } } export default system
src\views\learnVuex\index.vue
<template> <div> <my-addition ></my-addition> <p>----------------------</p> <my-subtranction ></my-subtranction> </div> </template> <script> // 導入 import Addition from '@/components/Addition'; import Subtranction from '@/components/Subtranction'; // import Subtranction from '../../components/Addition'; export default { name: 'learnVuex', props: {}, // 注冊 components: { 'my-addition': Addition, 'my-subtranction': Subtranction }, data() { return { } }, computed: {}, mounted(){ console.log("1111",this.$store.state.system.count); }, } </script> <style scoped> </style>
src\components\Addition\index.vue
<template> <div> <h3>當前最新的count值為:{{$store.state.system.count}}</h3> <h3>{{$store.getters['system/showNum']}}</h3> <el-button type="primary" @click="btnHandler1">+1</el-button> <el-button type="primary" @click="btnHandler2(2)">+2</el-button> <el-button type="primary" @click="btnHandler2(3)">+3</el-button> <el-button type="primary" @click="btnHandler3">+1 Async</el-button> <el-button type="primary" @click="btnHandler4">+3 Async</el-button> </div> </template> <script> export default { name: 'Addition', props: { }, data() { return { } }, computed: {}, mounted() { console.log('$store.state',this.$store.getters['system/showNum']); }, methods: { btnHandler1() { this.$store.commit("system/add") }, btnHandler2(val){ // commit 的作用就是調(diào)用某個mutation函數(shù) this.$store.commit("system/addN",val) }, // 異步的讓count自增+1 btnHandler3(){ // 這里的dispatch函數(shù),專門用來觸發(fā)actions this.$store.dispatch('system/addAsync') }, // btnHandler4(){ // 這里的dispatch函數(shù),專門用來觸發(fā)actions this.$store.dispatch('system/addNAsync',3) }, } } </script> <style scoped> </style>
\src\components\Subtranction\index.vue
<template> <div> <h3>當前最新的count值為:{{count}}</h3> <h3>{{showNum}}</h3> <el-button type="primary" @click="btnHandler1">-1</el-button> <el-button type="primary" @click="btnHandler2(2)">-2</el-button> <el-button type="primary" @click="btnHandler2(3)">-3</el-button> <el-button type="primary" @click="btnHandler3">-1 Async</el-button> <el-button type="primary" @click="btnHandler4">-3 Async</el-button> </div> </template> <script> import { mapState,mapMutations,mapActions,mapGetters } from 'vuex' export default { name: 'Subtranction', props: {}, data(){ return{ } }, computed: { ...mapState({ count: state => state.system.count, }), ...mapGetters({ showNum: 'system/showNum', }) }, mounted(){ console.log("mapState",this.count); }, methods:{ ...mapMutations({ sub: 'system/sub', subN: 'system/subN', }), ...mapActions({ subAsync: 'system/subAsync', subNAsync: 'system/subNAsync', }), btnHandler1(){ this.sub() }, btnHandler2(val){ this.subN(val) }, btnHandler3(){ this.subAsync() }, btnHandler4(){ this.subNAsync(3) }, } } </script> <style scoped> </style>
以上就是vue全家桶-vuex深入講解的詳細內(nèi)容,更多關于vue全家桶-vuex的資料請關注腳本之家其它相關文章!
相關文章
vue實現(xiàn)監(jiān)聽數(shù)值的變化,并捕捉到
這篇文章主要介紹了vue實現(xiàn)監(jiān)聽數(shù)值的變化,并捕捉到問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10vue.js的vue-cli腳手架中使用百度地圖API的實例
今天小編就為大家分享一篇關于vue.js的vue-cli腳手架中使用百度地圖API的實例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01