<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>demo</title> <!-- 引入animate.css--> <link href="https://cdn.jsdelivr.net/npm/animate.css@3. ...
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>demo</title> <!-- 引入animate.css--> <link href="https://cdn.jsdelivr.net/npm/[email protected]" rel="stylesheet" type="text/css"> <style> </style> </head> <body> <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script> <script src="https://cdn.staticfile.org/vue-router/2.7.0/vue-router.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/velocity/1.2.3/velocity.min.js"></script> <div id = "databinding"></div> <script type = "text/javascript"> var vm = new Vue({ el: '#databinding', data: { }, methods : { }, }); // 定義一個混入對象 var myMixin = { created: function () { this.startmixin() }, methods: { startmixin: function () { document.write("歡迎來到混入實例"); } } }; var Component = Vue.extend({ mixins: [myMixin] }) var component = new Component(); </script>
當組件和混入對象含有同名選項時,這些選項將以恰當的方式混合。
比如,數據對象在內部會進行淺合併 (一層屬性深度),在和組件的數據發生衝突時以組件數據優先。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>demo</title> <!-- 引入animate.css--> <link href="https://cdn.jsdelivr.net/npm/[email protected]" rel="stylesheet" type="text/css"> <style> </style> </head> <body> <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script> <script src="https://cdn.staticfile.org/vue-router/2.7.0/vue-router.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/velocity/1.2.3/velocity.min.js"></script> <div id = "databinding"></div> <script type = "text/javascript"> var mixin = { created: function () { document.write('混入調用' + '<br>') } } new Vue({ mixins: [mixin], created: function () { document.write('組件調用' + '<br>') } }); </script>
如果 methods 選項中有相同的函數名,則 Vue 實例優先順序會較高。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>demo</title> <!-- 引入animate.css--> <link href="https://cdn.jsdelivr.net/npm/[email protected]" rel="stylesheet" type="text/css"> <style> </style> </head> <body> <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script> <script src="https://cdn.staticfile.org/vue-router/2.7.0/vue-router.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/velocity/1.2.3/velocity.min.js"></script> <div id = "databinding"></div> <script type = "text/javascript"> var mixin = { methods: { hellworld: function () { document.write('HelloWorld 方法' + '<br>'); }, samemethod: function () { document.write('Mixin:相同方法名' + '<br>'); } } }; var vm = new Vue({ mixins: [mixin], methods: { start: function () { document.write('start 方法' + '<br>'); }, samemethod: function () { document.write('Main:相同方法名' + '<br>'); } } }); vm.hellworld(); vm.start(); vm.samemethod(); </script>
也可以全局註冊混入對象。
註意使用!
一旦使用全局混入對象,將會影響到 所有 之後創建的 Vue 實例。使用恰當時,可以為自定義對象註入處理邏輯。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>demo</title> <!-- 引入animate.css--> <link href="https://cdn.jsdelivr.net/npm/[email protected]" rel="stylesheet" type="text/css"> <style> </style> </head> <body> <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script> <script src="https://cdn.staticfile.org/vue-router/2.7.0/vue-router.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/velocity/1.2.3/velocity.min.js"></script> <div id = "databinding"></div> <script type = "text/javascript"> // 為自定義的選項 'myOption' 註入一個處理器。 Vue.mixin({ created: function () { var myOption = this.$options.myOption if (myOption) { document.write(myOption) } } }) new Vue({ myOption: 'hello!' }) </script>