wordpress文章列表 框網頁關鍵詞排名優(yōu)化
實現vuex源碼,手寫
Vuex 是專門為 Vue.js 應用程序開發(fā)的狀態(tài)管理模式 + 庫,它采用集中式存儲管理應用的所有組件的狀態(tài),并以相應的規(guī)則保證狀態(tài)以一種可預測的方式發(fā)生變化。
第一步:定義初始化Store類
創(chuàng)建文件夾store/vuex.js
1.定義 Store 類:
- 創(chuàng)建一個名為
Store
的類,它接受一個options
對象作為參數。 - 在
options
對象中,包含state
(應用的狀態(tài))、mutations
(同步更改狀態(tài)的方法)、actions
(異步操作或包含任意異步操作的方法)、以及getters
(從 state 中派生出一些狀態(tài)的方法)。
let Vue;
class Store{constructor(options) {}
}
2.初始化 Vue 實例:
- 在
Store
類的構造函數中,使用new Vue({ data: { $$state: options.state } })
創(chuàng)建一個 Vue 實例,用于響應式地存儲狀態(tài)。這里使用$$state
作為屬性的名稱是為了避免與 Vue 實例自身的state
屬性沖突,但這不是必須的,只是一個命名約定。
let Vue;
class Store{constructor(options) {this._vm = new Vue({data:{$$state:options.state}})}
}
3.存儲 mutations 和 actions:
- 將
options.mutations
和options.actions
分別存儲在this._mutations
和this._actions
中。
let Vue;
class Store{constructor(options) {this._vm = new Vue({data:{$$state:options.state}})this._mutations = options.mutationsthis._actions = options.actions}
}
4.綁定 commit 和 dispatch 方法:
- 使用
Function.prototype.bind
方法將commit
和dispatch
方法綁定到Store
實例上,以確保在回調函數中this
指向正確。
let Vue;
class Store{constructor(options) {this._vm = new Vue({data:{$$state:options.state}})this._mutations = options.mutationsthis._actions = options.actionsthis.commit = this.commit.bind(this)this.dispatch = this.dispatch.bind(this)}
}
5.初始化 getters:
- 創(chuàng)建一個空對象
this.getters
用于存儲 getter 方法。 - 如果
options.getters
存在,則調用this.handleGetters(options.getters)
方法來初始化 getters。
let Vue;
class Store{constructor(options) {this._vm = new Vue({data:{$$state:options.state}})this._mutations = options.mutationsthis._actions = options.actionsthis.commit = this.commit.bind(this)this.dispatch = this.dispatch.bind(this)this.getters = {}options.getters && this.hanleGetters(options.getters)}
}
第二步:實現 handleGetters 方法和其他 Store 方法
1.實現 handleGetters 方法:
- 在
handleGetters
方法中,遍歷getters
對象的鍵。 - 使用
Object.defineProperty
在this.getters
對象上定義每個 getter 屬性,其get
方法返回getters[key](this.state)
的結果。
let Vue;
class Store{constructor(options) {this._vm = new Vue({data:{$$state:options.state}})this._mutations = options.mutationsthis._actions = options.actionsthis.commit = this.commit.bind(this)this.dispatch = this.dispatch.bind(this)this.getters = {}options.getters && this.hanleGetters(options.getters)}handleGetters(getters){Object.key(getters).map((key)=>{Object.defineProperty(this.getters,key,get: () => getters[key](this.state)})})}
}
2.實現 state 的 getter 和 setter:
- 使用
get state()
方法來訪問 Vue 實例中存儲的狀態(tài)。 - 使用
set state(v)
方法來防止直接修改狀態(tài)(雖然在這里,setter 只是打印了一個錯誤消息)。
let Vue;
class Store{constructor(options) {this._vm = new Vue({data:{$$state:options.state}})this._mutations = options.mutationsthis._actions = options.actionsthis.commit = this.commit.bind(this)this.dispatch = this.dispatch.bind(this)this.getters = {}options.getters && this.hanleGetters(options.getters)}handleGetters(getters){Object.key(getters).map((key)=>{Object.defineProperty(this.getters,key,get: () => getters[key](this.state)})})}//get setget state(){return this._vm.data.$$state}set state(v) {console.error("please provide");}
}
3.實現 commit 方法:
commit
方法用于觸發(fā) mutations,它接受一個type
(mutation 的類型)和一個可選的payload
(傳遞給 mutation 的數據)。- 根據
type
從this._mutations
中找到對應的 mutation 方法,并調用它,傳入this.state
和payload
。
let Vue;
class Store{constructor(options) {this._vm = new Vue({data:{$$state:options.state}})this._mutations = options.mutationsthis._actions = options.actionsthis.commit = this.commit.bind(this)this.dispatch = this.dispatch.bind(this)this.getters = {}options.getters && this.hanleGetters(options.getters)}handleGetters(getters){Object.key(getters).map((key)=>{Object.defineProperty(this.getters,key,get: () => getters[key](this.state)})})}//get setget state(){return this._vm.data.$$state}set state(v) {console.error("please provide");}//commitcommit(type,value){const entry = this._mutations[type]if(!entry){console.error("please provide");}entry(this.state,value)}
}
4.實現 dispatch 方法:
dispatch
方法用于觸發(fā) actions,它的工作原理與commit
類似,但通常用于處理異步操作。
let Vue;
class Store{constructor(options) {this._vm = new Vue({data:{$$state:options.state}})this._mutations = options.mutationsthis._actions = options.actionsthis.commit = this.commit.bind(this)this.dispatch = this.dispatch.bind(this)this.getters = {}options.getters && this.hanleGetters(options.getters)}handleGetters(getters){Object.key(getters).map((key)=>{Object.defineProperty(this.getters,key,get: () => getters[key](this.state)})})}//get setget state(){return this._vm.data.$$state}set state(v) {console.error("unknown mutation type");}//commitcommit(type,value){const entry = this._mutations[type]if(!entry){console.error("please provide");}entry(this.state,value)}//dispatchdispatch(type,value){const entry = this._actions[type]if(!entry){console.error("unknown action type")}entry(this.state,value)}
}
第三步:安裝現在自定義vuex插件,需要一個install方法
- 創(chuàng)建一個名為
install
的函數,它接受一個 Vue 構造函數作為參數。 - 在
install
函數中,將 Vue 構造函數存儲在全局變量Vue
中。 - 使用
Vue.mixin
方法來全局注冊一個 beforeCreate 鉤子,該鉤子會在每個 Vue 組件實例創(chuàng)建之前被調用。 - 在 beforeCreate 鉤子中,檢查
this.$options.store
是否存在,如果存在,則將其賦值給Vue.prototype.$store
,這樣在任何 Vue 組件中都可以通過this.$store
訪問到 store 實例。
let Vue;
class Store{constructor(options) {this._vm = new Vue({data:{$$state:options.state}})this._mutations = options.mutationsthis._actions = options.actionsthis.commit = this.commit.bind(this)this.dispatch = this.dispatch.bind(this)this.getters = {}options.getters && this.hanleGetters(options.getters)}handleGetters(getters){Object.key(getters).map((key)=>{Object.defineProperty(this.getters,key,get: () => getters[key](this.state)})})}//get setget state(){return this._vm.data.$$state}set state(v) {console.error("unknown mutation type");}//commitcommit(type,value){const entry = this._mutations[type]if(!entry){console.error("please provide");}entry(this.state,value)}//dispatchdispatch(type,value){const entry = this._actions[type]if(!entry){console.error("unknown action type")}entry(this.state,value)}
}Store.install = (_vue)=>{Vue = _vueVue.mixin({beforeCreate(){if(this.$options.store){Vue.prototype.$store = this.$options.$store}}})
}
第四步:導出install,Store
let Vue;
class Store {constructor(options) {this._vm = new Vue({data: {$$state: options.state,},});this._mutations = options.mutations;this._actions = options.actions;this.commit = this.commit.bind(this);this.dispatch = this.dispatch.bind(this);this.getters = {};options.getters && this.handleGetters(options.getters);}handleGetters(getters) {console.log(Object.keys(getters))Object.keys(getters).map((key) => {Object.defineProperty(this.getters, key, {get: () => getters[key](this.state)});});}get state() {return this._vm._data.$$state;}set state(v) {console.error("please provide");}commit(type, payload) {console.log(type, payload)const entry = this._mutations[type];if (!entry) {console.error("unknown mutation type: " + type);}entry(this.state, payload);}dispatch(type, payload) {console.log(this._actions[type]);const entry = this._actions[type];if (!entry) {console.error("unknown mutation type: " + type);}entry(this.state, payload);}
}
const install = (_Vue) => {Vue = _Vue;Vue.mixin({beforeCreate() {if (this.$options.store) {Vue.prototype.$store = this.$options.store;}},});
};
export default {Store,install,
};
第五步:創(chuàng)建store/index.js
import Vue from 'vue'
// 引入自己的寫的vuex,里面有一個對象{install},當你use時,會自動調用這個方法
import Vuex from './vuex.js'
Vue.use(Vuex)
//需要創(chuàng)建一個倉庫并導出
//當new的時候,給Vuex.js中傳入了一堆的東西
export default new Vuex.Store({state: {name: 1},//getters中雖然是一個方法,但是用時,可以把他當作屬性getters: { // 說白了,就是vue中data中的computedpowerCount(state) {return state.name * 2},},// 改變狀態(tài):異步請求數據 事件 mutations: {add(state) {state.name++}},actions: {add(state) {setTimeout(() => {console.log(state)state.name = 30}, 1000);}}
})
第六步:在main中掛載store
/* eslint-disable vue/multi-word-component-names */
import Vue from 'vue'
import App from './App.vue'
import store from "./store.js"
Vue.config.productionTip = falsenew Vue({name:"main",store,render: h => h(App),
}).$mount('#app')
第七步:如何使用store
和vuex一樣的用法,語法一樣
<template><div>{{ $store.state.name }}{{ $store.getters.powerCount }}<button @click="add">123</button></div>
</template>
<script>
export default {name: "app",data() {return {}},methods:{add(){this.$store.commit('add')// this.$store.dispatch('add')console.log(this.$store.getters.powerCount)this.$store.handleGetters.powerCount}},mounted() { console.log(this.$store.state.name)}
}
</script>{{ $store.state.name }}{{ $store.getters.powerCount }}<button @click="add">123</button></div>
</template>
<script>
export default {name: "app",data() {return {}},methods:{add(){this.$store.commit('add')// this.$store.dispatch('add')console.log(this.$store.getters.powerCount)this.$store.handleGetters.powerCount}},mounted() { console.log(this.$store.state.name)}
}
</script>