语法:
Vue.component( ‘组件名称’,{ data:组件数据,——是一个函数 function(){ return { } } template:组件模板内容——是字符串,字符串当中可以写vue所支持的模板语法 })
例子:
<body> <div id="app"> <button-counter></button-counter> <button-counter></button-counter> <button-counter></button-counter> <button-counter></button-counter> </div> <script type="text/javascript" src="js/vue.js"></script> <script type="text/javascript"> //注册组件 Vue.component('button-counter',{ data: function(){ return { count:0 } }, //template:'<button @click="count++">点击了{{count}}次</button>', 这种也可以 template:'<button @click="handle">点击了{{count}}次</button>', //这里也是可以有methods的 methods:{ handle:function(){ this.count+=2; } } }) var vm = new Vue({ el:'#app', data:{ }, }) </script>template:’<button @click=“handle”>点击了{{count}}次< /button>< button>测试< /button> ’ 这样会报错,它们两个是兄弟关系,得在外层加个div才不会报错
组件模板内容可以是模板字符串的形式(可读性)命名格式 如果使用驼峰式命名组件,那么使用组件的时候,只能在字符串模板中用驼峰的方式使用组件,在普通的标签模板中,必须使用短横线的方式使用组件(所有单词都小写,每个单词之间用短横线连接)例子:
<body> <div id="app"> <component-tom></component-tom> <component-a></component-a> <component-lili></component-lili> </div> <script type="text/javascript" src="js/vue.js"></script> <script type="text/javascript"> //组件的内容可以抽取到一个对象当中 var componentA = { data:function(){ return { msg:'HelloWorld' } }, template:'<div>{{msg}}</div>', } var componenttom = { data:function(){ return { msg:'i am tom' } }, template:'<div>{{msg}}</div>', } var vm = new Vue({ el:'#app', data:{ }, components:{ 'component-a':componentA, //组件的内容可以抽取到一个对象当中 'component-tom':componenttom, } }) </script>组件名还是有加 ’ ’ 的
在父组件中,直接通过属性的静态方式或动态绑定的属性值的方式传递给子组件,然后子组件通过props接收属性名来接收
父组件通过属性将值传递给子组件< component-test title=‘来自父组件的值’>< /component-test> < component-test :title=‘trans’ multi=‘nihao’>< /component-test>
子组件内部通过props接收传递过来的值Vue.component(‘component-test’,{ props:[‘title’,‘multi’], data:function(){ return { zmsg:‘子组件本身的数据’ } }, template:’< div>{{zmsg + “----------”+ title + “--------”+multi}}< /div>’ })
props属性命名规则:
在props中使用驼峰形式,普通模板中需要使用短横线的形式,而在字符串形式的模板中没有这个限制子组件触发一个自定义事件,父组件去监听这个自定义事件
子组件通过自定义事件向父组件传递信息 $emit(‘自定义事件的名称’,需要传递的数据)触发事件父组件用v-on 监听子组件的事件,接收子组件传递过来的内容($event) <cart-list :list='list' @cart-del='delCart($event)'></cart-list>看回放叭
var hub = new Vue( );
监听事件与销毁事件hub.$on(‘监听的事件名称’,()=>{});
hub.$off(‘监听的事件名称’);
触发事件hub.$emit( '方法名‘,传递的数据)
组件内部什么时候去监听事件呢? mounted钩子函数什么时候触发? 点击按钮的时候触发对应的事件 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <div id="app"> <div>{{fmsg}}</div> <test-tom></test-tom> <test-jerry></test-jerry> <button @click='handle'>销毁事件</button> </div> <script type="text/javascript" src="js/vue.js"></script> <script type="text/javascript"> //事件中心 var hub = new Vue(); Vue.component('test-tom',{ data:function(){ return { num:0 } }, template:` <div> <div>TOM:{{num}}</div> <div> <button @click='handle'>点击</button> </div> </div> `, methods:{ handle:function(){ // 触发兄弟组件的事件 hub.$emit('jerry-event',2); } }, mounted:function(){ hub.$on('tom-event',(val)=>{ this.num+=val; }) } }); Vue.component('test-jerry',{ data:function(){ return { num:0 } }, template:` <div> <div>JERRY:{{num}}</div> <div> <button @click='handle'>点击</button> </div> </div> `, methods:{ handle:function(){ // 触发兄弟组件的事件 hub.$emit('tom-event',1) } }, mounted:function(){ hub.$on('jerry-event',(val)=>{ this.num += val; }) } }); var vm = new Vue({ el:'#app', data:{ fmsg:'父组件', }, methods:{ handle:function(){ hub.$off('tom-event'); hub.$off('jerry-event'); } } }) </script> </body> </html>例子: 父组件动态控制哪一种水果高亮 (因为子组件一旦封装好后,后期就很少在动了,所以最好有一种办法,从父组件可以直接去决定,这就是作用域插槽所解决的问题) 这个列表数据是通过动态遍历的方式生成的 所以我们需要提供对应数据。 对于数据来说,我们一般是通过父组件传递过来的,所以 ①在父组件中要提供一份水果列表的数据。②父组件通过属性绑定的方式传递给子组件 ③子组件通过props接收,然后遍历
<div id="app"> <fruit-list :list='list'> <!-- 通过插槽传递过来的属性 子组件通过属性名传递过来数据--> <template slot-scope='slotProps'> <strong v-if='slotProps.info.id==3' class="current">{{slotProps.info.name}}</strong> <span v-else>{{slotProps.info.name}}</span> </template> </fruit-list> </div> <script type="text/javascript" src="js/vue.js"></script> <script type="text/javascript"> Vue.component('fruit-list',{ props:['list'], template:` <div> <li :key='item.id' v-for='item in list'> <slot :info='item'> {{item.name}} </slot> </li> </div> `, }) var vm = new Vue({ el:'#app', data:{ list:[{ id:1, name:'pear', },{ id:2, name:'orange', },{ id:3, name:'strawberry' }] } }) </script>首先,在子组件的模板当中要提供一个插槽,slot中间填充我们要显示的数据,并且slot标签当中要绑定一个属性,(属性名字自定义,用来提供给父组件。属性值是你要操作的数据) 然后,从父组件的角度:要填充slot。 填充规则:在组件标签的中间提供内容,这个内容用< template>标签进行包裹,并且通过template的一个属性:slot-scope来得到子组件中传递过来的数据,数据得到后就可以进行一些控制了
