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>
  • 相关阅读:
    react给input元素中文输入的时候自动转成字符串bug
    charles代理
    es6和es5函数参数和arguments的差别
    Uncaught InvalidStateError: Failed to set the 'value' property on 'HTMLInputElement': This input element accepts a filename, which may only be programmatically set to the empty string.
    Node 开发概述
    对Flutter路由管理库Fluro的封装
    Flutter 切换标签显示对应的列表+Provide状态管理实例
    Flutter 商城实例 分类列表
    Flutter 入口页面及底部导航栏实例制作
    Flutter 建立项目和编写入口文件
  • 原文地址:https://www.cnblogs.com/Merrys/p/12744291.html
Copyright © 2011-2022 走看看