Vue组件及通信方式

Vue组件及通信方式

Vue组件及通信方式

一. 声明组件与子组件,以及他们之间的通信方式

一). 父-子:自定义属性

1.代码<body><!– 挂载点 –><divid=“app”></div><!– 父组件的模板代码 –><templateid=“parent”><counterusername=“admin”email=“496542445@qq.com”/></template><!– 子组件的模板代码 –><templateid=“child”><p>UserName:{{username}}</p><p>UserEmail:{{email}}</p></template><s专业系统搭建点我wcqh.cncript>const app =Vue.createApp({// 父组件 template:`#parent`,}); app.component(“counter”,{// 接受父组件的传过来的参数 props:[“username”,“email”], template:`#child`,});const vm = app.mount(“#app”);</script></body>2.实现效果图

二).子-父:自定义事件

1.代码<body><!– 挂载点 –><divid=“app”><counter @review=“review”/></div><templateid=“counter”><but专业系统搭建点我wcqh.cnton @click=“count++”>点赞:+{{count}}</button><button @click=“review()”>评论</button></template><script>const app =Vue.createApp({ methods:{ review(count){ console.log(count);},},}); app.component(“counter”,{ template:`#counter`, data(){return{ count:0,};}, methods:{ review(){this.$emit(“review”,this.count);},},});co专业系统搭建点我wcqh.cnnst vm = app.mount(“#app”);// 1. 父-子:自定义属性// 2.子-父:自定义事件</script></body>2.实现的效果图

批改老师:PHPz 批改状态:合格 老师批语:总结的不错, 继续加油

© 版权声明
THE END
喜欢就支持一下吧
点赞886 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容