vue元件:和@區別 冒號和@符號區別
阿新 • • 發佈:2022-02-07
冒號:相當於引數,在元件裡面用this.方法名(引數) 直接呼叫
@符號:相當於事件方法,需要用this.$emit('方法名', "引數1","引數2"。。。)呼叫
<html> <head> <meta charset="UTF-8"> <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script> <script> Vue.component('myComponent', { props: { onFun1: { type: Function } }, methods: { componentFun1:function () { if(this.onFun1)this.onFun1("冒號"); console.log("-component.fun1.冒號直接呼叫") }, componentFun2: function () { this.$emit('on-fun2', "@符號") console.log("-component.fun1.@符號用$emit呼叫") }, }, template: `<span><button @click="componentFun1">fun1</button><button @click="componentFun2">fun2</button></span>` }); </script> </head> <body> <div id="app"> <span>{{index}}</span> <my-component :on-fun1="fun1" @on-fun2="fun2"></my-component> </div> <script> varv1 = new Vue({ el: '#app', data: { index:0, }, methods: { fun1: function (v) { this.index++; console.log("-page.fun1."+v); }, fun2: function (v) { this.index++; console.log("-page.fun2."+v); }, } }) </script> </body> </html>
歡迎加入JAVA技術交流QQ群:179945282
歡迎加入ASP.NET(C#)交流QQ群:17534377