【Vue 極速指南】Vuex 篇
在這篇文章,你將快速學習到:
如何安裝
什麼是 Vuex
Hello World
-
核心概念
mapState Helper
mapState Helper
State
Getters
Mutations
Actions
Modules
如何安裝
-
標準安裝
npm install vuex
什麼是 Vuex
Vuex 是一個專為 Vue.js 應用程式開發的狀態管理模式。它採用集中式儲存管理應用的所有元件的狀態,並以相應的規則保證狀態以一種可預測的方式發生變化。
Hello World
引入 vue.js & vuex.js
建立 index.html
建立 Vuex.Store():
store = new Vuex.Store({})
提交變化:
store.commit('increment')
<!DOCTYPE html><html><head> <meta charset="utf8"> <meta name="viewport" content="minimal-ui,width=device-width,initial-scale=1.0, maximum-scale=1.0,minimum-scale=1.0,user-scalable=no" /> <title>Hello Vuex</title> <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="vue.js"></script> <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="vuex.js"></script></head><body> <script> var store = new Vuex.Store({ state: { count: 0 }, mutations: { increment: function(state){ state.count++; } } }); store.commit('increment'); console.log(store.state.count); </script></body></html>
核心概念
State
建立 Vuex.Store()
定義 state.count:
{ state: { count: 0 }}
在元件中使用:
this.$store.state.count
<!DOCTYPE html><html><head> <meta charset="utf8"> <meta name="viewport" content="minimal-ui,width=device-width,initial-scale=1.0, maximum-scale=1.0,minimum-scale=1.0,user-scalable=no" /> <title>Vuex State</title> <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="vue.js"></script> <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="vuex.js"></script></head><body> <div id="app"> {{ count }} </div> <script> var store = new Vuex.Store({ state: { count: 1 } }); var app = new Vue({ el: '#app', store: store, computed: { count: function(){ return this.$store.state.count } } }); </script></body></html>
Getters
建立 Vuex.Store()
宣告 getters.doneTodos:
{ getters: { doneTodos: ... }}
在元件中使用:
this.$store.getters.doneTodos
<!DOCTYPE html><html><head> <meta charset="utf8"> <meta name="viewport" content="minimal-ui,width=device-width,initial-scale=1.0, maximum-scale=1.0,minimum-scale=1.0,user-scalable=no" /> <title>Vuex Getter</title> <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="vue.js"></script> <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="vuex.js"></script></head><body> <div id="app"> {{ doneTodos }} </div> <script> var store = new Vuex.Store({ state: { todos: [ {id: 1, text: 'a thing', done: true }, {id: 2, text: 'B thing', done: false } ] }, getters: { doneTodos: function(state){ return state.todos.filter(function(todo){ return todo.done; }); } } }); var app = new Vue({ el: '#app', store: store, computed: { doneTodos: function(){ return this.$store.getters.doneTodos } } }); </script></body></html>
Mutations
建立 Vuex.Store()
宣告 mutations.increment:
{ mutations: { increment: function(){} }}
在元件中使用:
this.$store.commit('increment')
<!DOCTYPE html><html><head> <meta charset="utf8"> <meta name="viewport" content="minimal-ui,width=device-width,initial-scale=1.0, maximum-scale=1.0,minimum-scale=1.0,user-scalable=no" /> <title>Vuex Mutation</title> <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="vue.js"></script> <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="vuex.js"></script></head><body> <div id="app"> {{ count }} <button @click="increment">increment</button> </div> <script> var store = new Vuex.Store({ state: { count: 1 }, mutations: { increment: function(state) { state.count++; } } }); var app = new Vue({ el: '#app', store: store, computed: { count: function() { return this.$store.state.count } }, methods: { increment: function() { this.$store.commit('increment'); } } }); </script></body></html>
Actions
create Vuex.Store()
宣告 actions.increment:
{ actions: { increment: function(){} }}
在元件中使用:
this.$store.dispatch('increment')
<!DOCTYPE html><html><head> <meta charset="utf8"> <meta name="viewport" content="minimal-ui,width=device-width,initial-scale=1.0, maximum-scale=1.0,minimum-scale=1.0,user-scalable=no" /> <title>Vuex Action</title> <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="vue.js"></script> <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="vuex.js"></script></head><body> <div id="app"> {{ count }} <button @click="increment">increment</button> </div> <script> var store = new Vuex.Store({ state: { count: 1 }, mutations: { increment: function(state) { state.count++; } }, actions: { increment: function(context) { context.commit('increment'); } } }); var app = new Vue({ el: '#app', store: store, computed: { count: function() { return this.$store.state.count } }, methods: { increment: function() { this.$store.dispatch('increment'); } } }); </script></body></html>
Modules
建立 moduleA & moduleB
在兩個模組中建立 Vuex.Store() :
new Vuex.Store({modules: {a: moduleA,b: moduleB}})
在元件中使用:
this.$store.state.a
〖堅持的一俢〗
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/4479/viewspace-2822797/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- TypeScript極速完全進階指南-2中級篇TypeScript
- vuex4 極速入門到上手Vue
- JavaScript 裝飾器極速指南JavaScript
- vue+vuex+leaflet Jest單測踩坑指南Vue
- Vue 教程第十五 篇—— Vuex 之 getterVue
- Vue 教程第十三篇—— Vuex 之 stateVue
- vue - VuexVue
- Vue--VuexVue
- 極速上手 VUE 3—v-model 的使用變化Vue
- vuex - 基礎篇Vue
- 極速指南:在 SpringBoot 中快速整合騰訊雲簡訊功能Spring Boot
- vue2.x+vuex+TS專案實戰基礎篇Vue
- VueX Vue: Could not find a declaration file for module vuex.Vue
- vue中vuex,echarts,地圖,ueditor的使用(一篇就夠)VueEcharts地圖
- Vue —— VueX精講(1)Vue
- vue實踐之vuexVue
- vue狀態共享--VUEXVue
- Vue踩坑之旅——VuexVue
- CMake極速入門
- Mybatis極速入門MyBatis
- vue+vue-router+vuex地址管理思路Vue
- vue 學習筆記 - vuexVue筆記
- vue升級之路(四)-- VuexVue
- Vue入門到跑路---VuexVue
- vue實踐04之vuexVue
- Vue+Vue-router+Vuex專案實戰Vue
- 極速體驗SpringCloud GatewaySpringGCCloudGateway
- 【譯】Vue 的小奇技(第六篇):在 Vue.js 2.6 中不使用 Vuex 來建立 storeVue.js
- VUE全家桶之vuex的使用Vue
- 【vue-系列】深入理解VuexVue
- Vue教程23:Vuex非同步ActionVue非同步
- Vuex——Vue的狀態管理利器Vue
- vue--vuex 中 Modules 詳解Vue
- [Vuex系列] - Module的用法(終篇)Vue
- 【譯】Vue 的小奇技(第八篇):兩個鮮有人知的 Vuex 技巧Vue
- Vuex 4與狀態管理實戰指南Vue
- Gin框架系列01:極速上手框架
- 極速精簡 Go 版 LogstashGo