<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        詳解幾十行代碼實現一個vue的狀態管理

        來源:懂視網 責編:小采 時間:2020-11-27 22:01:21
        文檔

        詳解幾十行代碼實現一個vue的狀態管理

        詳解幾十行代碼實現一個vue的狀態管理:介紹 采用集中式存儲管理應用的所有組件的狀態, 就能實現組件間數據共享 實現 邏輯圖 從圖上有兩條線: Vue.use(vuec), 與 new Vuec.center(options) 第一條線Vue.use(vuec)安裝插件 使用Vue.use(vuec)時, 會執行vuec的install
        推薦度:
        導讀詳解幾十行代碼實現一個vue的狀態管理:介紹 采用集中式存儲管理應用的所有組件的狀態, 就能實現組件間數據共享 實現 邏輯圖 從圖上有兩條線: Vue.use(vuec), 與 new Vuec.center(options) 第一條線Vue.use(vuec)安裝插件 使用Vue.use(vuec)時, 會執行vuec的install

        介紹

        采用集中式存儲管理應用的所有組件的狀態, 就能實現組件間數據共享

        實現

        邏輯圖

        從圖上有兩條線: Vue.use(vuec), 與 new Vuec.center(options)

        第一條線Vue.use(vuec)安裝插件

        使用Vue.use(vuec)時, 會執行vuec的install方法,會注入參數Vue 所以vuec是這樣的,

        // index.js
        import {Center, install} from './center'
        export default {Center, install}

        Center對象將實例化成center(下面再說),我們先看看install方法

        // center.js
        let Vue // 全局變量, 保存install里的Vue
        export function install (_Vue) {
         if (!Vue) {
         _Vue.mixin({
         beforeCreate: applyMixin // 在beforeCreate鉤子上混入applyMixin函數
         })
         }
         Vue = _Vue
        }
        
        

        install在Vue原型的beforeCreate混入applyMixin函數, 也就是說在生成每個Vue組件時,在它的生命周期beforeCreate鉤子上就會執行applyMixin方法

        第二條線 new Vuec.center(options)實例化Center對象

        先看看用戶傳入的options, 下面例子

        export default new Vuec.Center({
         state: {
         name: 'liuyang'
         },
         mutations: {
         changeName (state) {
         state.name = 'jike'
         }
         }
        })
        
        

        上面代碼會生成center實例, 該實例上應該包括:state狀態,commit方法提交變更等

        // center.js
        export class Center {
         constructor (options= {}) {
         let center = this
         this.mutations = options.mutations
         observeState(center, options.state)
         }
         get state () { // 代理了this.$center.state的最終訪問值
         return this._vm.$data.$$state
         }
         commit (_type, _payload) {
         this.mutations[_type](this.state, _payload)
         }
        }
        function observeState(center, state) { // 響應式state
         center._vm = new Vue({
         data: {
         $$state: state
         }
         })
        }

        在執行new Vuec.Center({..})時,就是執行Center的構造函數

        首先執行let center = this, 定義center保存當前實例

        接著執行this.mutations = options.mutations, 在實例center上添加mutations屬性, 值就是用戶輸入mutations,

        按上面例子, this.mutations長成這樣

        this.mutations = {
         changeName (state) {
         state.name = 'jike'
         }
        }
        
        

        最后執行observeState(center, options.state), 作用:讓center實例的state屬性指向options.state并且是響應式的

         function observeState(center, state) { // 響應式state
         center._vm = new Vue({ // 利用Vue的響應系統,將state轉化成響應式
         data: {
         $$state: state
         }
         })
         }

        在center實例上添加_vm屬性, 值是一個Vue實例, 在該Vue實例的data下定義了$$state, 它的值是options.state用戶輸入的state; 結合上面的這段代碼

        // center.js
        export class Center {
         ...省略
         get state () { // 代理了this.$center.state的最終訪問值
         return this._vm.$data.$$state
         }
         ...省略
        }
        
        

        所以我們在組件中訪問center.state其實就是訪問center._vm.$data.$$state

        OK, center就構建好了

        創建Vue組件

        用戶輸入

        import Vue from 'vue'
        import App from './App'
        import router from './router'
        import center from './center'
        
        new Vue({
         el: '#app',
         router,
         center, // 構建好的center實例
         template: '<App/>',
         components: {App}
        })
        
        

        在beforeCreate生命周期時會觸發上面混入的applyMixin函數

        // mixins.js
        export default function applyMixin() {
         vuecInit.call(this) // 
        }
        
        function vuecInit () {
         const options = this.$options
         // vue的實例化是從外往內, 所以父組件的$center一定是options的center
         this.$center = options.parent?options.parent.$center: options.center
        }
        
        

        applyMixin里會執行vuecInit.call(this), 這里的this指向當前組件的實例,

        接著看vuecInit, 定義了options等于用戶輸入選項,因為先創建根組件, 所以根組件this.$center的值的引用就是我們在new Vue({..center})時傳入的center實例, 下面所有組件都指向它

        OK, 你就可以在組件里使用this.$center訪問了

        commit變更

        // center.js
        export class Center {
         ... 省略
         commit (_type, _payload) {
         this.mutations[_type](this.state, _payload)
         }
        }
        

        通常我們變更時: this.$center.commit('changeName', 'jike'), 這樣的話, this.mutations[_type]就是對應方法函數, 往該函數里傳入state以及payload,

        舉上面的例子

        // this.mutations[_type] , _type = 'changeName', payload= 'jike'
        this.mutations = {
         changeName (state, payload) {
         state.name = payload
         }
        }
        
        

        說明

        上面只是一個簡單的狀態管理, 還有很多地方沒有實現: actions異步變更,getters函數,modules模塊分割, 輔助函數mapState..等

        源碼地址: github

        聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        詳解幾十行代碼實現一個vue的狀態管理

        詳解幾十行代碼實現一個vue的狀態管理:介紹 采用集中式存儲管理應用的所有組件的狀態, 就能實現組件間數據共享 實現 邏輯圖 從圖上有兩條線: Vue.use(vuec), 與 new Vuec.center(options) 第一條線Vue.use(vuec)安裝插件 使用Vue.use(vuec)時, 會執行vuec的install
        推薦度:
        標簽: VUE 狀態 管理
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 日本xxxx色视频在线观看免费| 无码精品人妻一区二区三区免费 | 久久久久久精品免费看SSS| 永久免费不卡在线观看黄网站| 中文字幕乱码免费看电影| h视频在线观看免费网站| 亚洲电影一区二区三区| 亚洲经典在线观看| 精品女同一区二区三区免费播放 | 亚洲午夜无码久久| 一区二区三区免费在线视频| 免费a级毛片18以上观看精品| 亚洲色欲久久久综合网| 亚洲精品中文字幕麻豆| 国产免费女女脚奴视频网| 精品亚洲成α人无码成α在线观看| 日本久久久久亚洲中字幕| 亚洲AV性色在线观看| 久久久久国色av免费看| 亚洲A∨午夜成人片精品网站| 亚洲日本一区二区| 丁香六月婷婷精品免费观看| 成人免费大片免费观看网站| 亚洲一区电影在线观看| 抽搐一进一出gif免费视频| 青青草国产免费久久久91| 亚洲av无码国产精品色午夜字幕 | 毛片a级三毛片免费播放| 国产亚洲精品岁国产微拍精品| 亚洲AV性色在线观看| 亚洲一本大道无码av天堂| 亚洲码欧美码一区二区三区| av无码国产在线看免费网站 | 亚洲一本大道无码av天堂| 免费国产叼嘿视频大全网站| 亚洲精品tv久久久久| 亚洲依依成人亚洲社区| 亚洲av日韩av欧v在线天堂| 国产精成人品日日拍夜夜免费| 麻豆国产VA免费精品高清在线| 一个人看的www在线免费视频|