zoukankan      html  css  js  c++  java
  • vue-10-混合

    混合对象可以包含任意组件选项,

    // 定义一个混合对象
    var myMixin = {
      created: function () {
        this.hello()
      },
      methods: {
        hello: function () {
          console.log('hello from mixin!')
        }
      }
    }
    // 定义一个使用混合对象的组件
    var Component = Vue.extend({
      mixins: [myMixin]
    })
    
    //创建实例
    var component = new Component() // => "hello from mixin!"

     选项合并:同名钩子函数都将被调用,混合对象的钩子将在组件自身钩子之前调用 

    var mixin = {
      created: function () {
        console.log('混合对象的钩子被调用')
      }
    }
    
    new Vue({
      mixins: [mixin],
      created: function () {
        console.log('组件钩子被调用')
      }
    })

    值为对象的选项,被混合为同一个对象。两个对象键名冲突时,取组件对象的键值对,Vue.extend() 也使用同样的策略进行合并

    var mixin = {
      methods: {
        conflicting: function () {
          console.log('from mixin')
        }
      }
    }
    
    var vm = new Vue({
      mixins: [mixin],
      methods: {
        conflicting: function () {
          console.log('from self')
        }
      }
    })
    vm.conflicting() // => "from self"

    全局混合:

    //会影响到每个单独创建的 Vue 实例 (包括第三方模板),可以将其用作 Plugins 以避免产生重复应用
    
    Vue.mixin({
      created: function () {
        var myOption = this.$options.myOption
        if (myOption) {
          console.log(myOption)
        }
      }
    })
    new Vue({
      myOption: 'hello!'
    })

    自定义选项合并策略

    Vue.config.optionMergeStrategies.myOption = function (toVal, fromVal) {
      // return mergedVal
    }
    
    //大多数时候使用methods的合并策略
    var strategies = Vue.config.optionMergeStrategies
    strategies.myOption = strategies.methods
    
    
    //使用computed的合并策略
    const merge = Vue.config.optionMergeStrategies.computed
    Vue.config.optionMergeStrategies.vuex = function (toVal, fromVal) {
      if (!toVal) return fromVal
      if (!fromVal) return toVal
      return {
        getters: merge(toVal.getters, fromVal.getters),
        state: merge(toVal.state, fromVal.state),
        actions: merge(toVal.actions, fromVal.actions)
      }
    }
  • 相关阅读:
    Lock
    synchronized
    线程可见性与原子性
    线程安全问题
    MySQL索引背后的数据结构和原理
    求一颗二叉树中两个节点的最低公共父节点
    Session not active, could not store state 的解决方法
    https nginx 设置
    第三方支付系统
    facebook页面种简单测试js调用flash开放的js接口的方法
  • 原文地址:https://www.cnblogs.com/avidya/p/7640350.html
Copyright © 2011-2022 走看看