var 混入对象名 = {
created: 操作
};
new Vue({
component: {
mixins: [混用对象名],
}
DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>CROW-songtitle>
<script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js">script>
head>
<body>
<div id="databinding">div>
<script type="text/javascript">
new Vue({
el: "#databinding",
component: {
mixins: [myMixin],
}
});
// 定义一个混入对象
var myMixin = {
created: document.write("混入实例"),
};
script>
body>
html>
var 混入对象名 = {
created: 操作
};
var 组件名 = Vue.extend({
mixins: [myMixin],
});
new 组件名();
DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>CROW-songtitle>
<script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js">script>
head>
<body>
<div id="databinding">div>
<script type="text/javascript">
var vm = new Vue({
el: "#databinding",
});
// 定义一个混入对象
var myMixin = {
created: document.write("混入实例"),
var Component = Vue.extend({
mixins: [myMixin],
});
new Component();
script>
body>
html>
将上例替换如下:
var myMixin = {
created: function () {
document.write("混入实例");
},
};
DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>CROW-songtitle>
<script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js">script>
head>
<body>
<div id="databinding">div>
<script type="text/javascript">
var vm = new Vue({
el: "#databinding",
data: {},
methods: {},
});
var myMixin = {
created: function () {
//引用methods中函数
this.startmixin();
},
methods: {
startmixin: function () {
document.write("混入实例");
},
},
};
var Component = Vue.extend({
mixins: [myMixin],
});
var component = new Component();
script>
body>
html>
Vue 实例与混入对象包含了相同的方法。两个函数将合并,并且一起输出。
DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>CROW-宋title>
<script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js">script>
head>
<body>
<div id="databinding">div>
<script type="text/javascript">
var mixin = {
created: function () {
document.write("混入调用" + "
");
},
};
new Vue({
mixins: [mixin],
created: function () {
document.write("组件调用" + "
");
},
});
script>
body>
html>
混入调用
组件调用
Vue实例和混入对象实例中的methods下有同名函数,则Vue实中的methods下的同名函数优先级高
说明:
- 混入对象:methods中包含两个方法,一个无同名的(mixinfunction),一个和vue实例中同名的(samemethod)。
- vue实例:methods包含两个方法,一个无同名的(vmfunction),一个和混入对象中同名的(samemethod)。
按预期,mixinfunction、vmfunction和vue实例中的 samemethod 将被打印出来
DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>CROW-SONGtitle>
<script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js">script>
head>
<body>
<div id="databinding">div>
<script type="text/javascript">
var mixin = {
methods: {
mixinfunction: function () {
document.write("混入对象中的方法" + "
");
},
samemethod: function () {
document.write("混入对象中的同名方法" + "
");
},
},
};
var vm = new Vue({
mixins: [mixin],
methods: {
vmfunction: function () {
document.write("Vue实例中的方法" + "
");
},
samemethod: function () {
document.write("Vue中同名方法" + "
");
},
},
});
vm.mixinfunction();
vm.vmfunction();
vm.samemethod();
script>
body>
html>
混入对象中的方法
Vue实例中的方法
Vue中同名方法
DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>CROW-SONGtitle>
<script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js">script>
head>
<body>
<script type="text/javascript">
// 为自定义的选项 'myOption' 注入一个处理器。
Vue.mixin({
created: function () {
var myOption = this.$options.vueOption;
document.write(myOption);
},
});
new Vue({
vueOption: "HELLO !",
});
script>
body>
html>
HELLO !