国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

當前位置: 首頁 > news >正文

wordpress文章列表 框網頁關鍵詞排名優(yōu)化

wordpress文章列表 框,網頁關鍵詞排名優(yōu)化,金華集團網站建設,做網站商城開發(fā)什么語言最快實現vuex源碼,手寫 Vuex 是專門為 Vue.js 應用程序開發(fā)的狀態(tài)管理模式 庫,它采用集中式存儲管理應用的所有組件的狀態(tài),并以相應的規(guī)則保證狀態(tài)以一種可預測的方式發(fā)生變化。 第一步:定義初始化Store類 創(chuàng)建文件夾store/vuex.js 1…

實現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.mutationsoptions.actions 分別存儲在 this._mutationsthis._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 方法將 commitdispatch 方法綁定到 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.definePropertythis.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 的數據)。
  • 根據 typethis._mutations 中找到對應的 mutation 方法,并調用它,傳入 this.statepayload。
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>
http://aloenet.com.cn/news/44306.html

相關文章:

  • 直播網站開發(fā)系統(tǒng)優(yōu)化的意義
  • 佛山網站建設電話seo工作職責
  • 國外做3d h視頻網站天津網站優(yōu)化
  • 深圳seo網站優(yōu)化公司seo中介平臺
  • 營銷網站建設套餐合肥seo快排扣費
  • 男人做想看的免費網站全渠道營銷成功案例
  • 做網站要會寫代碼嗎百度關鍵詞搜索怎么弄
  • 最好免費觀看高清播放seo發(fā)帖網站
  • 上海做網站那家公司好如何創(chuàng)建一個app平臺
  • 網站建設與開發(fā)試卷新東方培訓機構官網
  • 怎么做好網站方式推廣免費私人網站建設
  • 交互式網站有哪些功能友情鏈接出售
  • 備案網站轉入阿里云管理方面的培訓課程
  • 阿里云做網站搜索引擎有哪些分類
  • 做優(yōu)惠卷網站倒閉了多少錢剪輯培訓班一般學費多少
  • 企業(yè)網站營銷優(yōu)缺點搜索
  • 架設一個網站太原今日新聞最新頭條
  • 使用網站效果圖b站推廣軟件
  • 制作團購網站搜索引擎優(yōu)化關鍵詞的處理
  • iis怎么建網站最新的網絡營銷的案例
  • 東莞長安網站設計軟件培訓班
  • 無錫 網站建設公司北京做網站公司哪家好
  • 網站菜單素材湖北seo
  • 網頁游戲網站斗地主青島seo關鍵詞優(yōu)化公司
  • 新媒體營銷案例有哪些百度seo如何優(yōu)化關鍵詞
  • 網站設為主頁功能怎么做下載谷歌瀏覽器并安裝
  • 網絡規(guī)劃的內容廣東網站營銷seo費用
  • 惠州建設網站搜索引擎營銷案例分析
  • wordpress 關閉自動保存功能seo自動排名軟件
  • 浙江高端網站熱點新聞