大佬教程收集整理的这篇文章主要介绍了VUE学习二十,组件初步component,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
一、先看下面的代码
<!DOCTYPE html> <html> <head> <@H_36_16@meta charset="utf-8"> <title></title> <script src="../js/vue.js" type="text/javascript" charset="utf-8"></script> </head> <body> <div id="app"> <button-counter title="title1 : " @clicknow="clicknow"> <h2>hi...h2</h2> </button-counter> <button-counter title="title2 : "></button-counter> </div> <script type="text/javascript"> Vue.component('button-counter', { props: ['title'], data: function () { return { count: 0 } }, template: '<div><h1>hi...</h1><button v-on:click="clickfun">{{titlE}} You clicked me {{ count }} times.</button><slot></slot></div>', methods:{ clickfun : function () { this.count ++; this.$emit('clicknow', this.count); } } }) var vm = new Vue({ el : "#app", data : { }, methods:{ clicknow : function (E) { console.log(E); } } }); </script> <style type="text/css"> </style> </body> </html>
二、讲解
1. 组件注册
// 定义一个名为 button-counter 的新组件 Vue.component('button-counter', { data: function () { return { count: 0 } }, template: '<button v-on:click="count++">You clicked me {{ count }} times.</button>' })
2. 组件引用
<div id="components-demo"> <button-counter></button-counter> </div>
new Vue({ el: '#components-demo' })
3. 组件复用
<div id="components-demo"> <button-counter></button-counter> <button-counter></button-counter> <button-counter></button-counter> </div>
4 .data 必须是一个函数
这样,每个实例可以维护一份被返回对象的独立的拷贝。
5 .通过 prop 向子组件传递数据
6. 每个组件必须只有一个根元素
7. 增加单击事件
8. 增加插槽slot
template: '<div><h1>hi...</h1><button v-on:click="clickfun">{{titlE}} You clicked me {{ count }} times.</button><slot></slot></div>',
本文参考:
https://cn.vuejs.org/v2/guide/components.html
以上是大佬教程为你收集整理的VUE学习二十,组件初步component全部内容,希望文章能够帮你解决VUE学习二十,组件初步component所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。