zoukankan      html  css  js  c++  java
  • Vuex实现

    • 新创建一个class类实现Vuex基本功能
    • 将store注入Vue实例和Vue组件中
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
    </head>
    
    <body>
      <div id="app">
        <span>{{num}}</span>
        <button @click='add'>增加1</button>
        <button @click='asyncAdd'>异步增加1</button>
        <Child />
      </div>
    </body>
    <script src="./vue.js"></script>
    <script>
      // 实现Vuex
      class Store {
        constructor(options) {
          this.vm = new Vue({ // 实例化一个Vue对象,将 外面 Vuex的参数传进来
            data() {
              return { state: options.state } // 将 Vuex的参数 state 传进来,为的是实现数据的监听
            }
          })
    
          let getters = options.getters;
          this.getters = {}
          Object.keys(getters).forEach(getterName => {
            Object.defineProperty(this.getters, getterName, {
              get: () => {
                return getters[getterName](this.vm.state)
              }
            })
          })
          let mutations = options.mutations;
          this.mutations = {};
          Object.keys(mutations).forEach(mutationName => {
            this.mutations[mutationName] = (payload) => {
              mutations[mutationName](this.state, payload)
            }
          })
          let actions = options.actions;
          this.actions = {};
          Object.keys(actions).forEach(actionName => {
            this.actions[actionName] = (payload) => {
              actions[actionName].call(this, this, payload)
            }
          })
        }
    
        get state() {
          return this.vm.state;
        }
    
        commit(mutationName, payload) {
          this.mutations[mutationName](payload);
        }
    
        dispatch(actionName, payload) {
          this.actions[actionName](payload);
        }
      }
    
      // 实例化一个Vuex
      let store = new Store({
        state: { num: 1 },
        getters: {
          getNum: function (state) {
            return state.num
          }
        },
        mutations: {
          add(state, payload) {
            state.num = state.num + payload;
          }
        },
        actions: {
          asyncAdd({ commit }, payload) {
            setTimeout(() => {
              this.commit('add', payload)
            }, 3000)
          }
        }
      })
    
      //在Vue实例创建时插入Vue实例和Vue组件中
      Vue.mixin({
        beforeCreate() {
          if (this.$options && this.$options.store) {
            this.$store = this.$options.store;
          } else {
            this.$store = this.$parent && this.$parent.$store;
          }
        },
      })
    
      var Child = {
        template: '<h1>{{$store.state.num}}</h1>'
      }
    
      new Vue({
        el: '#app',
        components: { Child },
        computed: {
          num() {
            return this.$store.state.num
          }
        },
        store: store,
        methods: {
          add() {
            this.$store.commit('add', 1)
          },
          asyncAdd() {
            this.$store.dispatch('asyncAdd', 10)
          }
        },
        created() {
        }
      })
    </script>
    
    </html>
  • 相关阅读:
    自定义异常
    finally关键字
    捕捉异常try-catch
    throws抛出异常
    exception概述和分类
    jvm前奏篇
    Java并发编程学习随笔 (一) 使用run() 和 start()的差别
    MyCat学习 ------分库分表 随笔
    java最常用的内置工具类
    Mybatis框架常见面试题
  • 原文地址:https://www.cnblogs.com/Merrys/p/12744291.html
Copyright © 2011-2022 走看看