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

// 定义一个混合对象
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)
}
}

vue-10-混合的更多相关文章

  1. [MAUI] 在.NET MAUI中结合Vue实现混合开发

    ​ 在MAUI微软的官方方案是使用Blazor开发,但是当前市场大多数的Web项目使用Vue,React等技术构建,如果我们没法绕过已经积累的技术,用Blazor重写整个项目并不现实. Vue是当前流 ...

  2. vue的混合mixins学习

    mixins   混合 (mixins) 是一种分发 Vue 组件中可复用功能的非常灵活的方式.   混合对象可以包含任意组件选项.   当组件使用混合对象时,所有混合对象的选项将被混入该组件本身的选 ...

  3. 使用cordova + vue搭建混合app框架

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/zxj0904010228/article ...

  4. 前端框架之Vue(10)-全家桶简单使用实例

    vue-router官方文档 vuex官方文档 安装 npm install vue-router --save 使用实例 vue-router初使用(webpack-simple模板) 1.切换到指 ...

  5. vue app混合开发蓝牙串口连接(报错java.io.IOException: read failed, socket might closed or timeout, read ret: -1;at android.bluetooth.BluetoothSocket.connect at js/BluetoothTool.js:329)

    我使用的uni-app <template> <view class="bluetooth"> <!-- 发送数据 --> <view c ...

  6. vue之父子组件之间的通信方式

    (一)props与$emit <!-这部分是一个关于父子组件之间参数传递的例子--> <!--父组件传递参数到子组件是props,子组件传递参数到父组件是用事件触发$emit--&g ...

  7. Vue(五)Vue规范

    代码规范很重要 1.组件名应该始终是多个单词的,根组件 App 除外. 2.组件的 data 必须是一个函数. // In a .vue file export default { data () { ...

  8. 教你用Cordova打包Vue项目

    现在国内越来越多的开发者使用Vue开发混合app,但是当大家开发完成过后才发现不知道该怎么将Vue项目打包成app. 据我现在的了解打包Vue项目目前流行的就是使用weex和cordova.weex是 ...

  9. vue+cordova项目

    教你用Cordova打包Vue项目   现在国内越来越多的开发者使用Vue开发混合app,但是当大家开发完成过后才发现不知道该怎么将Vue项目打包成app.据我现在的了解打包Vue项目目前流行的就是使 ...

  10. vue全局loading组件

    本组件作用在页面加载完成前进行loader提示,提升用户体验,只需要在app.vue中引用一次,整个项目中路由切换时就可以自动进行提示(vuex版): 1. 添加vuex值和方法: import Vu ...

随机推荐

  1. [MySQL]典型的行列转换

    列变成行 测试数据库数据样式: 应用的sql语句: SELECT TM,NAME,SUM(GE) AS 'GE',SUM(GD) AS 'GD',SUM(CT) AS 'CT',SUM(NUM) AS ...

  2. Python全栈开发-Day4-Python基础4

    本节内容 匿名函数 装饰器 列表生成式.迭代器&生成器 内置函数 Json & pickle 数据序列化 1. 匿名函数 匿名函数就是不需要显式的指定函数 1 2 3 4 5 6 7 ...

  3. Run keyword if

    Wait For Page Ready ${a} Run Keyword And Return Status Page Should Contain 新建 log ${a} Run Keyword I ...

  4. 基于 Spring Cloud 完整的微服务架构实战

    本项目是一个基于 Spring Boot.Spring Cloud.Spring Oauth2 和 Spring Cloud Netflix 等框架构建的微服务项目. @作者:Sheldon地址:ht ...

  5. 算法:最短路径之弗洛伊德(Floyd)算法

    https://cloud.tencent.com/developer/article/1012420 为了能讲明白弗洛伊德(Floyd)算法的主要思想,我们先来看最简单的案例.图7-7-12的左图是 ...

  6. java8新特性: lambda表达式:直接获得某个list/array/对象里面的字段集合

    java8新特性: lambda表达式:直接获得某个list/array/对象里面的字段集合 比如,我有一张表: entity Category.java service CategoryServic ...

  7. 基于AngularJS的Onsen UI --Onsen UI学习笔记

    AngularJS与Onsen UI的结合,Onsen UI应用程序实际上是一个AngularJS 1应用程序. <!doctype html><html lang="en ...

  8. 使用Vue cli3搭建一个用Fetch Api的组件

    系列参考 ,英文原文参考 我的git代码: https://github.com/chentianwei411/Typeahead 目标: 建立一个输入关键字得到相关列表的组件,用Vuejs2和Fet ...

  9. Codeforces 1151F Sonya and Informatics (概率dp)

    大意: 给定01序列, 求随机交换k次后, 序列升序的概率. 假设一共$tot$个$0$, 设交换$i$次后前$tot$个数中有$j$个$0$的方案数为$dp[i][j]$, 答案即为$\frac{d ...

  10. sublime 3的破解和安装

    http://www.xue51.com/mac/1518.html 啥都别问,问就是按照上面的网址操作就行,本人亲测可用.