vue组件:和@区别 冒号和@符号区别
冒号:相当于参数,在组件里面用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> var v1 = 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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步