对组件的理解
[TOC]
Q:组件的构成
- 完整web端组件由高度解耦的html、css、js构成
- 组件的包涵4个生命周期:初始化、渲染、事件绑定、销毁
- 合理的接口设计
Q:Vue组件
一个简单的vue组件必须包含:
- props:传递数据
- template: html模版
- $emit:事件传递
- activated:组件渲染完成
- deactivated:组件注销
<div id="app">
<keep-alive>
<child-component :text="text" @on="callblock"></child-component>
</keep-alive>
<button onclick="destroy()">destroy</button>
</div>
<script type="text/template" id="ChildComponent">
<div v-if="show">
<h1>{{text}}</h1>
<button @click="hanldClick">emit</button>
</div>
</script>
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
var childComponent = Vue.extend({
name:'childComponents',
template: document.getElementById('ChildComponent'),
props:{
text:{
type: String
}
},
data:function(){
return {
show: true,
}
},
methods:{
hanldClick(){
console.log('hanldClick:'+this.text);
this.$emit('on',this.text);
},
},
activated(){
console.group('activated');
console.log(this.$el)
console.groupEnd();
},
deactivated(){
console.group('deactivated');
console.log(this.$el)
console.groupEnd();
}
});
var vm = new Vue({
el: '#app',
data: {
text: 'hello world'
},
components:{
childComponent: childComponent
},
methods:{
callblock:function(val){
console.log('callblock:'+val);
},
}
});
function destroy(){
vm.$destroy();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
Q:Web Components
web components包含:
- Shadow DOM
- 自定义元素
- HTML 模板
<template></template> - HTML 导入
← 元素水平垂直居中的方法9种 vue面试题 →