16.混入

61 阅读1分钟

混入

混入其实就是为了提取公共代码,效果其实类似于把混入搞了个父类,然后子类都是可以轻松继承copy一份代码。

有的时候,许多组件有着类似的功能,这些功能代码分散在组件不同的配置中。

image-20210105161811637

于是,我们可以把这些配置代码抽离出来,利用混入融合到组件中。

image-20210105162109015

具体的做法非常简单:

// 抽离的公共代码
const common = {
  data(){
    return {
      a: 1,
      b: 2
    }
  },
  created(){
    console.log("common created");
  },
  computed:{
    sum(){
      return this.a + this.b;
    }
  }
}

/**
 * 使用comp1,将会得到:
 * common created
 * comp1 created 1 2 3
 */
const comp1 = {
  mixins: [common] // 之所以是数组,是因为可以混入多个配置代码
  created(){
    console.log("comp1 created", this.a, this.b, this.sum);
  }
}

混入并不复杂,更多细节参见官网