File tree Expand file tree Collapse file tree 4 files changed +110
-10
lines changed Expand file tree Collapse file tree 4 files changed +110
-10
lines changed Original file line number Diff line number Diff line change 16
16
-->
17
17
<!-- 准备好一个容器-->
18
18
< div id ="root ">
19
- < Hello > </ Hello >
20
- < School > </ School >
19
+ < App > </ App >
21
20
</ div >
22
21
23
22
< script type ="text/javascript " >
24
23
Vue . config . productionTip = false //关闭生产提示
24
+
25
+ //向Vue的原型对象上追加一个xyz属性,值为1
26
+ Vue . prototype . xyz = 1
27
+
25
28
/*
26
29
定义一个School组件:
27
30
1.如何定义一个组件?-- 使用Vue.extend(options)去创建
40
43
(2).Vue.extend调用的返回值是VueComponent构造函数,所以new School其实就是在new VueComponent。
41
44
(3).所谓组件实例,就是VueComponent创建的实例,简称vc。
42
45
所有Vue实例,就是Vue创建的实例,简称vm
46
+ (4).组件的data函数、以及methods中配置的函数中this都是vc
43
47
6.一个最最最重要的关系:
44
48
VueComponent继承了Vue,所以Vue.prototype上的属性和方法,vc都能看得见
45
49
*/
46
-
47
50
const School = Vue . extend ( {
48
51
//data中存放组件所需数据
49
52
data ( ) {
53
+ console . log ( '@' , this )
50
54
return {
51
55
name :'尚硅谷' ,
52
56
address :'北七家镇-宏福科技园'
53
57
}
54
58
} ,
59
+ methods : {
60
+ showAddress ( ) {
61
+ console . log ( this . address )
62
+ }
63
+ } ,
55
64
//template中配置组件的模板结构
56
65
template :`
57
66
<div>
58
- <h2>学校名:{{name}}</h2>
59
- <h2>学校地址:{{address}}</h2>
67
+ <h2 class="title" >学校名:{{name}}</h2>
68
+ <h2 @click="showAddress" >学校地址:{{address}}</h2>
60
69
</div>
61
70
`
62
71
} )
63
- Vue . prototype . xyz = 1
72
+
64
73
//定义个Hello组件
65
74
const Hello = Vue . extend ( {
66
75
template :`<h2>Hello</h2>`
67
76
} )
68
77
78
+ const App = Vue . extend ( {
79
+ components :{ School, Hello} ,
80
+ template :`
81
+ <div>
82
+ <Hello/>
83
+ <School/>
84
+ </div>
85
+ `
86
+ } )
87
+
69
88
//注册School组件----全局注册
70
89
// Vue.component('School',School)
71
90
72
91
//定义一个vm,去管理所有的组件
73
92
const vm = new Vue ( {
74
93
el :'#root' ,
75
94
//注册School组件----局部注册----以后工作用的多
76
- components :{
77
- School,
78
- Hello
79
- }
95
+ components :{ App}
80
96
} )
81
97
</ script >
82
98
</ body >
Original file line number Diff line number Diff line change
1
+ /* 配置组件的结构 */
2
+ <template >
3
+ <div >
4
+ <h2 >我是App,我有一台{{car}}</h2 >
5
+ <School />
6
+ </div >
7
+ </template >
8
+
9
+ /* 配置组件数据、交互、事件等等*/
10
+ <script >
11
+ // 引入School组件
12
+ import School from ' ./components/School'
13
+
14
+ export default {
15
+ data (){
16
+ return {
17
+ car: ' 马自达阿特兹'
18
+ }
19
+ },
20
+ components: {School},
21
+ }
22
+ </script >
Original file line number Diff line number Diff line change
1
+ /*
2
+ 较真来讲:当前的这个.vue文件,并不是一个组件,而是一个组件的所有配置
3
+ */
4
+
5
+ /* 配置组件模板结构 */
6
+ <template >
7
+ <div >
8
+ <h2 class =" title" class =" title" >学校名:{{name}}</h2 >
9
+ <h2 class =" info" @click =" showAddress" >学校地址:{{address}}</h2 >
10
+ </div >
11
+ </template >
12
+
13
+ /* 配置组件数据、交互、事件等等 */
14
+ <script >
15
+ // 此处只是暴露了一个组件的配置,并没有去创建组件,因为没有调用Vue.extend
16
+ export default {
17
+ // data中存放组件所需数据
18
+ data (){
19
+ return {
20
+ name: ' 尚硅谷' ,
21
+ address: ' 北七家镇-宏福科技园'
22
+ }
23
+ },
24
+ methods: {
25
+ showAddress (){
26
+ alert (this .address )
27
+ }
28
+ }
29
+ }
30
+ </script >
31
+
32
+ /* 配置组件样式 */
33
+ <style >
34
+ .title {
35
+ background-color : orange ;
36
+ }
37
+ .info {
38
+ background-color : pink ;
39
+ }
40
+ </style >
Original file line number Diff line number Diff line change
1
+ <!DOCTYPE html>
2
+ < html >
3
+ < head >
4
+ < meta charset ="UTF-8 " />
5
+ < title > </ title >
6
+ < script type ="text/javascript " src ="../js/vue.js "> </ script >
7
+ </ head >
8
+ < body >
9
+ <!-- 准备好一个容器-->
10
+ < div id ="root ">
11
+ < App />
12
+ </ div >
13
+
14
+ < script type ="text/javascript " >
15
+ import App from './App.vue'
16
+
17
+ new Vue ( {
18
+ components :{ App}
19
+ } ) . $mount ( '#root' )
20
+ </ script >
21
+ </ body >
22
+ </ html >
You can’t perform that action at this time.
0 commit comments