vue基礎(chǔ)入門之vuex安裝與使用
本教程為入門教程,如有錯(cuò)誤,請各位前端大佬指出。
1.什么是vuex
Vuex 是一個(gè)專為 Vue.js 應(yīng)用程序開發(fā)的狀態(tài)管理模式。它采用集中式存儲管理應(yīng)用的所有組件的狀態(tài),并以相應(yīng)的規(guī)則保證狀態(tài)以一種可預(yù)測的方式發(fā)生變化.詳細(xì)介紹可以參照官網(wǎng)文檔vuex.vuejs.org/zh/
下面簡單介紹vuex
2.安裝和引入
先安裝vuex。
npm install vuex --save
在main.js中引入后即可使用。
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
//vuex使用
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
//全局變量
count: 31231
}
})
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
//vuex必須加入
store,
components: { App },
template: '<App/>'
})
3.vuex的使用
<template>
<div>
老大有{{showData}}
<HelloWorld2/>
</div>
</template>
<script>
import HelloWorld2 from './HelloWorld2'
import son from './son'
export default {
name: 'HelloWorld',
data () {
return {
message2:"",
cou
}
},
components:{
HelloWorld2,
son
},computed: {
showData(){
return this.$store.state.count;
}
}
}
</script>
<template>
<div>
老二有{{$store.state.count}}
</div>
</template>
<script>
export default {
name: 'HelloWorld2',
data() {
return {
}
}
}
</script>
4.流程介紹

如圖當(dāng)沒有使用vuex時(shí)流程為: view->actions->state->view

使用了vuex后流程為vuecomponent->(dispatch)actions->(commit)mutations->(mutate)state->(render)->vuecomponent
5.mutation
狀態(tài)更改,更改 Vuex 的 store 中的狀態(tài)的唯一方法是提交mutation。Vuex 中的 mutation 非常類似于事件:每個(gè) mutation 都有一個(gè)字符串的事件類型 (type)和一個(gè)回調(diào)函數(shù) (handler)。這個(gè)回調(diào)函數(shù)就是我們實(shí)際進(jìn)行狀態(tài)更改的地方,并且它會接受 state 作為第一個(gè)參數(shù)。
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
//vuex使用
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
//全局變量
count: 31231
},
//更改狀態(tài)方法
mutations: {
//state為上面的state
addData(state) {
// 變更狀態(tài)
state.count++
}
}
})
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
//vuex必須加入
store,
components: { App },
template: '<App/>'
})
然后執(zhí)行更改
<template>
<div>
老大有{{showData}}
<HelloWorld2/>
<button type = "button" v-on:click = "changeData"> 修改按鈕 </button>
</div>
</template>
<script>
import HelloWorld2 from './HelloWorld2'
import son from './son'
export default {
name: 'HelloWorld',
data () {
return {
message2:"",
}
},
components:{
HelloWorld2,
son
},computed: {
showData(){
return this.$store.state.count;
}
},
methods: {
//執(zhí)行更改
changeData(event){
this.$store.commit("addData");
}
}
}
</script>
6.getters過濾
可以限制mutation 比如小于0就不能減少了
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
//vuex使用
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
//全局變量
count: 0
},
//更改狀態(tài)方法
mutations: {
//state為上面的state
addData(state) {
// 變更狀態(tài)
state.count++
}
},
//過濾
getters: {
getState(state) {
if (state.count >= 5) {
return 5
} else {
return state.count
}
}
}
})
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
//vuex必須加入
store,
components: { App },
template: '<App/>'
})
調(diào)用時(shí)
<template>
<div>
老二有{{$store.getters.getState}}
</div>
</template>
<script>
export default {
name: 'HelloWorld2',
data() {
return {
}
}
}
</script>
7.Action--異步處理
Action 類似于 mutation,不同在于:
Action 提交的是 mutation,而不是直接變更狀態(tài)。 Action 可以包含任意異步操作。 mutation只能同步處理
main.js。示例如下:
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
//vuex使用
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
//全局變量
count: 0
},
//更改狀態(tài)方法
mutations: {
//state為上面的state
addData(state) {
// 變更狀態(tài)
state.count++
}
},
//過濾
getters: {
getState(state) {
if (state.count >= 5) {
return 5
} else {
return state.count
}
}
},
actions: {
//action觸發(fā)的mutations方法 優(yōu)勢是異步處理
addData(context) {
//模擬異步
setTimeout(() => {
context.commit('addData')
}, 1000)
}
}
})
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
//vuex必須加入
store,
components: { App },
template: '<App/>'
})
在發(fā)送時(shí) 應(yīng)該調(diào)用action。
<template>
<div>
老大有{{showData}}
<HelloWorld2/>
<button type = "button" v-on:click = "changeData"> 修改按鈕 </button>
</div>
</template>
<script>
import HelloWorld2 from './HelloWorld2'
import son from './son'
export default {
name: 'HelloWorld',
data () {
return {
message2:"",
}
},
components:{
HelloWorld2,
son
},computed: {
showData(){
return this.$store.getters.getState;
}
},
methods: {
//執(zhí)行更改
changeData(event){
//操作mutations方法
//this.$store.commit("addData");
//應(yīng)該操作action而不是action觸發(fā)的mutations方法
this.$store.dispatch("addData");
}
}
}
</script>
8.Module
由于使用單一狀態(tài)樹,應(yīng)用的所有狀態(tài)會集中到一個(gè)比較大的對象。當(dāng)應(yīng)用變得非常復(fù)雜時(shí),store 對象就有可能變得相當(dāng)臃腫。
為了解決以上問題,Vuex 允許我們將 store 分割成模塊(module)。每個(gè)模塊擁有自己的 state、mutation、action、getter、甚至是嵌套子模塊——從上至下進(jìn)行同樣方式的分割:
如路由可以分割文件 不在main.js中放入vuex 新建store/index.js
//vuex使用
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
//全局變量
count: 0
},
//更改狀態(tài)方法
mutations: {
//state為上面的state
addData(state) {
// 變更狀態(tài)
state.count++
}
},
//過濾
getters: {
getState(state) {
if (state.count >= 5) {
return 5
} else {
return state.count
}
}
},
actions: {
//action觸發(fā)的mutations方法 優(yōu)勢是異步處理
addData(context) {
//模擬異步
setTimeout(() => {
context.commit('addData')
}, 1000)
}
}
})
修改main.js
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
//vuex必須加入
store,
components: { App },
template: '<App/>'
})
我們還能把main.js中的state拿出 新建store/state.js
export default {
count: 0
}
然后index.js可以改成
//vuex使用
import Vue from 'vue'
import Vuex from 'vuex'
import state from './state'
Vue.use(Vuex)
export default new Vuex.Store({
state: state,
//更改狀態(tài)方法
mutations: {
//state為上面的state
addData(state) {
// 變更狀態(tài)
state.count++
}
},
//過濾
getters: {
getState(state) {
if (state.count >= 5) {
return 5
} else {
return state.count
}
}
},
actions: {
//action觸發(fā)的mutations方法 優(yōu)勢是異步處理
addData(context) {
//模擬異步
setTimeout(() => {
context.commit('addData')
}, 1000)
}
}
})
總結(jié)
到此這篇關(guān)于vue入門之vuex安裝與使用的文章就介紹到這了,更多相關(guān)vuex安裝與使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3.x?的shallowReactive?與?shallowRef?使用場景分析
在Vue3.x中,`shallowReactive`和`shallowRef`是用于創(chuàng)建淺層響應(yīng)式數(shù)據(jù)的API,它們與`reactive`和`ref`類似,本文介紹vue3.x??shallowReactive?與?shallowRef的使用場景,感興趣的朋友一起看看吧2025-02-02
如何解決Vue請求接口出現(xiàn)跨域問題Access-Control-Allow-Origin
這篇文章主要介紹了如何解決Vue請求接口出現(xiàn)跨域問題Access-Control-Allow-Origin,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
SpringBoot+Vue3實(shí)現(xiàn)上傳文件功能
這篇文章主要介紹了SpringBoot+Vue3實(shí)現(xiàn)上傳文件功能,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-01-01
Vue引入vuetify框架你需要知道的幾點(diǎn)知識
這篇文章主要介紹了Vue引入vuetify框架你需要知道的幾點(diǎn)知識,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
vue中wangEditor的使用及回顯數(shù)據(jù)獲取焦點(diǎn)的方法
最近在寫vue的項(xiàng)目中,遇到一個(gè)需求,點(diǎn)擊編輯,顯示彈框,在彈框中的富文本編輯器中編輯自定義文本樣式,可以上傳圖片并回顯。接下來通過本文給大家介紹vue中wangEditor的使用及回顯數(shù)據(jù)獲取焦點(diǎn)的問題,一起看看吧2021-09-09
solid.js響應(yīng)式createSignal 源碼解析
這篇文章主要為大家介紹了solid.js響應(yīng)式createSignal 源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09

