文章涉及的內容可能不全面,但量不少,須要慢慢看。我花了很長的時間整理,用心分享心得,但願對你們有所幫助。可是不免會有打字的錯誤或理解的錯誤點,但願發現的能夠郵箱告訴我1163675970@qq.com,我會及時的進行修改,只但願對你有所幫助,謝謝。javascript
Vuex 是一個專門爲 vue.js 應用程序開發的狀態(狀態就是數據)管理模式,它採用集中式存儲管理應用的狀態。至關於把組件中的數據提高到一個全局的地方,這個地方就是 Vuex 的 store(倉庫),由 Vuex 統一管理,vue
若是某個組件須要這個數據,直接從 store 中獲取。 若是要修改存在 Vuex 中的數據,須要在定義 store 時,定義修改這個數據的方法,這些方法稱爲 mutation;java
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
// state 就是數據,若是數據定義在 state 中組件中若是要使用這個數據 this.$store.state.屬性名 的方式獲取
num: 15
},
mutations: {
// state 中的數據不能被直接修改,若是要修改這些數據,須要使用 mutation,注意 mutation 中不能使用異步更新 state
add (state) {
// mutation 函數的第一個參數是 state 對象,全部的數據都定義 state 中,在 mutation 函數中經過 state 能夠修改 上面 state 中的數據;
state.num++
},
addNum (state, payload) {
// mutation 的第二個參數 payload 是在 commit mutation 時傳入的參數
state.num += payload
}
},
actions: {
// action 可使用異步,可是更新數據仍然須要 commit 對應的 mutation
asyncAdd(context) {
setTimeout(() => {
context.commit('add')
}, 1000)
}
}
})
複製代碼
<template>
<div id="app"> <div id="nav"> <router-link to="/">Home</router-link> | <router-link to="/about">About</router-link> <h1>someNum: {{$store.state.num}}</h1> </div> <router-view/> </div> </template> <script> import Bus from './bus' export default { name: 'App', data () { return {} } } </script> 複製代碼
<template>
<div class="home">
<img alt="Vue logo" src="../assets/logo.png">
<div>
<button @click="add">給上面的sumNum加加</button>
<button @click="add2">給上面的sumNum加2</button>
</div>
</div>
</template>
<script>
export default {
name: 'home',
methods: {
add () {
this.$store.commit('addOne') // commit mutation
this.$store.dispatch('asyncAdd') // dispatch action
},
add2 () {
this.$store.commit('addNum', 2) // 傳遞 payload
}
}
}
</script>
複製代碼
若是使用不少個 store 中的數據和方法時經過 this.$store 的方式很不方便,Vuex 提供了三個對應的方法用於批量獲取多個 state、mutation、action;vuex
<script>
// @ is an alias to /src
import HelloWorld from '@/components/HelloWorld.vue'
// import Bus from '../bus'
import { mapState, mapMutations, mapActions } from 'vuex'
export default {
name: 'home',
methods: {
updateX (e) {
this.$store.commit('changeX', e.target.value)
},
updateSex(e) {
this.$store.commit('changeSex', e.target.value)
},
add () {
this.addOne() // 等效於 this.$store.commit('addOne')
},
add2 () {
this.addNum(2); // 等效於 this.$store.commit('addNum', 2)
},
add3 () {
this.asyncAdd2(); // 等效於 this.$store.dispatch('asyncAdd')
},
...mapMutations(['addOne', 'addNum']),
...mapActions({
asyncAdd2: 'asyncAdd' // 重命名
})
},
computed: {
...mapState(['num'])
},
components: {
HelloWorld
}
}
</script>
複製代碼
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
import * as homeActions from './model/home'
export default new Vuex.Store({
state: {
slides: [],
hotBooks: []
},
mutations: {
fillSides (state, payload) {
state.slides = payload
},
fillHotBooks (state, payload) {
state.hotBooks = payload
}
},
actions: {
async getSliders ({ commit }) {
commit('fillSides', await homeActions.getSliders())
},
async getHotBooks ({ commit }) {
commit('fillHotBooks', await homeActions.getHot())
}
}
})
複製代碼
<template>
<div >
<MyHeader>首頁</MyHeader>
<div class="content">
<swiper :sliders="slides"></swiper>
<div class="container">
<h2>熱門圖書</h2>
<ul class="container">
<li v-for="(book, index) in hotBooks" :key="index">
<img :src="book.bookCover" alt="">
<b>
{{book.bookName}}
</b>
</li>
</ul>
</div>
</div>
</div>
</template>
<script>
// @ is an alias to /src
import MyHeader from '@/components/MyHeader.vue'
import swiper from '@/components/Swiper.vue'
// import { getSliders, getHot } from '../model/home'
import { mapState, mapActions } from 'vuex'
export default {
name: 'home',
data () {
return {}
},
computed: {
// slides 和 hotBooks 經過 mapState 從 Vuex store 中獲取
...mapState(['slides', 'hotBooks'])
},
created () {
// 經過 Vuex 獲取圖書和輪播信息
this.getSliders(); // this.$store.dispatch('getSliders')
this.getHotBooks(); // this.$store.dispatch('getHotBooks')
},
methods: {
...mapActions(['getSliders', 'getHotBooks'])
},
components: {
MyHeader,
swiper
}
}
</script>
<style scoped lang="less">
.container {
box-sizing: border-box;
overflow-x: hidden;
h2 {
padding-left: 30px;
}
ul li {
float: left;
width: 50%;
margin: 20px 0;
img {
display: block;
}
b {
padding-left: 30px;
}
}
}
</style>
複製代碼