File tree Expand file tree Collapse file tree 3 files changed +67
-4
lines changed Expand file tree Collapse file tree 3 files changed +67
-4
lines changed Original file line number Diff line number Diff line change 11
11
1.定义组件:Vue.extend(options)
12
12
2.注册组件:
13
13
全局注册:Vue.component('组件名',组件)
14
- 局部注册
14
+ 局部注册:配置components属性,components:{组件名:组件}
15
15
3.写组件标签
16
16
-->
17
17
<!-- 准备好一个容器-->
18
18
< div id ="root ">
19
+ < Hello > </ Hello >
19
20
< School > </ School >
20
21
</ div >
21
22
33
34
(3).组件的模板结构,要配置在template属性中:
34
35
(3.1)值为html字符串,而且要用模板字符串。
35
36
(3.2)模板结构,必须只有一个跟标签。
37
+ 4.所有的组件定义后,必须注册才能使用,注册分为:全局注册、局部注册
38
+ 5.特别注意:
39
+ (1).School确实是构造函数,但不是我们亲手写的School,是Vue.extend生成的。
40
+ (2).Vue.extend调用的返回值是VueComponent构造函数,所以new School其实就是在new VueComponent。
41
+ (3).所谓组件实例,就是VueComponent创建的实例,简称vc。
42
+ 所有Vue实例,就是Vue创建的实例,简称vm
43
+ 6.一个最最最重要的关系:
44
+ VueComponent继承了Vue,所以Vue.prototype上的属性和方法,vc都能看得见
36
45
*/
46
+
37
47
const School = Vue . extend ( {
38
48
//data中存放组件所需数据
39
49
data ( ) {
@@ -50,16 +60,22 @@ <h2>学校地址:{{address}}</h2>
50
60
</div>
51
61
`
52
62
} )
63
+ Vue . prototype . xyz = 1
64
+ //定义个Hello组件
65
+ const Hello = Vue . extend ( {
66
+ template :`<h2>Hello</h2>`
67
+ } )
53
68
54
69
//注册School组件----全局注册
55
70
// Vue.component('School',School)
56
71
57
72
//定义一个vm,去管理所有的组件
58
- new Vue ( {
73
+ const vm = new Vue ( {
59
74
el :'#root' ,
60
- //注册School组件----局部注册
75
+ //注册School组件----局部注册----以后工作用的多
61
76
components :{
62
- School
77
+ School,
78
+ Hello
63
79
}
64
80
} )
65
81
</ script >
Original file line number Diff line number Diff line change
1
+ <!DOCTYPE html>
2
+ < html >
3
+ < head >
4
+ < meta charset ="UTF-8 " />
5
+ < title > Document</ title >
6
+ </ head >
7
+ < body >
8
+ < script type ="text/javascript " >
9
+
10
+ //模拟Vue.extend
11
+ /* const Vue = {
12
+ extend(){
13
+ function VueComponent(){
14
+
15
+ }
16
+ return VueComponent
17
+ }
18
+ }
19
+ const School = Vue.extend()
20
+ const s = new School()
21
+ console.log(s) // */
22
+ </ script >
23
+ </ body >
24
+ </ html >
Original file line number Diff line number Diff line change
1
+ <!DOCTYPE html>
2
+ < html >
3
+ < head >
4
+ < meta charset ="UTF-8 " />
5
+ < title > Document</ title >
6
+ </ head >
7
+ < body >
8
+ < script type ="text/javascript " >
9
+
10
+ class Person { }
11
+ Person . prototype . b = 200
12
+
13
+ class Student extends Person {
14
+ name = 'tom'
15
+ }
16
+ Student . prototype . a = 100 //a想给s用
17
+
18
+ const s = new Student ( )
19
+
20
+ console . log ( s . b )
21
+ </ script >
22
+ </ body >
23
+ </ html >
You can’t perform that action at this time.
0 commit comments