vue基礎入門之vuex安裝與使用
本教程為入門教程,如有錯誤,請各位前端大佬指出。
1.什么是vuex
Vuex 是一個專為 Vue.js 應用程序開發(fā)的狀態(tài)管理模式。它采用集中式存儲管理應用的所有組件的狀態(tài),并以相應的規(guī)則保證狀態(tài)以一種可預測的方式發(fā)生變化.詳細介紹可以參照官網文檔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.流程介紹
如圖當沒有使用vuex時流程為: 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 非常類似于事件:每個 mutation 都有一個字符串的事件類型 (type)和一個回調函數(shù) (handler)。這個回調函數(shù)就是我們實際進行狀態(tài)更改的地方,并且它會接受 state 作為第一個參數(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/>' })
調用時
<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ā)送時 應該調用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"); //應該操作action而不是action觸發(fā)的mutations方法 this.$store.dispatch("addData"); } } } </script>
8.Module
由于使用單一狀態(tài)樹,應用的所有狀態(tài)會集中到一個比較大的對象。當應用變得非常復雜時,store 對象就有可能變得相當臃腫。
為了解決以上問題,Vuex 允許我們將 store 分割成模塊(module)。每個模塊擁有自己的 state、mutation、action、getter、甚至是嵌套子模塊——從上至下進行同樣方式的分割:
如路由可以分割文件 不在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) } } })
總結
到此這篇關于vue入門之vuex安裝與使用的文章就介紹到這了,更多相關vuex安裝與使用內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue中的attribute和property的具體使用及區(qū)別
本文主要介紹了vue中的attribute和property的具體使用及區(qū)別,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09Echarts實現(xiàn)一張圖現(xiàn)切換不同的X軸(實例代碼)
這篇文章主要介紹了Echarts 如何實現(xiàn)一張圖現(xiàn)切換不同的X軸,通過動圖給大家展示效果,實例代碼相結合給大家介紹的非常詳細,需要的朋友可以參考下2021-11-11Vue.js監(jiān)聽select2的值改變進行查詢方式
這篇文章主要介紹了Vue.js監(jiān)聽select2的值改變進行查詢方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04vue?element-ui的table列表中展示多張圖片(可放大)效果實例
這篇文章主要給大家介紹了關于vue?element-ui的table列表中展示多張圖片(可放大)效果的相關資料,文中通過代碼示例介紹的非常詳細,需要的朋友可以參考下2023-08-08vue3使用el-radio-group獲取表格數(shù)據(jù)無法選中問題及解決方法
這篇文章主要介紹了vue3使用el-radio-group獲取表格數(shù)據(jù)無法選中問題及解決方法,本文給大家介紹的非常詳細,需要的朋友可以參考下2024-05-05