Skip to content

Commit c62b4a0

Browse files
committed
更新
1 parent 7f25390 commit c62b4a0

File tree

4 files changed

+110
-10
lines changed

4 files changed

+110
-10
lines changed

18_组件_非单文件/非单文件组件.html

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@
1616
-->
1717
<!-- 准备好一个容器-->
1818
<div id="root">
19-
<Hello></Hello>
20-
<School></School>
19+
<App></App>
2120
</div>
2221

2322
<script type="text/javascript" >
2423
Vue.config.productionTip = false //关闭生产提示
24+
25+
//向Vue的原型对象上追加一个xyz属性,值为1
26+
Vue.prototype.xyz = 1
27+
2528
/*
2629
定义一个School组件:
2730
1.如何定义一个组件?-- 使用Vue.extend(options)去创建
@@ -40,43 +43,56 @@
4043
(2).Vue.extend调用的返回值是VueComponent构造函数,所以new School其实就是在new VueComponent。
4144
(3).所谓组件实例,就是VueComponent创建的实例,简称vc。
4245
所有Vue实例,就是Vue创建的实例,简称vm
46+
(4).组件的data函数、以及methods中配置的函数中this都是vc
4347
6.一个最最最重要的关系:
4448
VueComponent继承了Vue,所以Vue.prototype上的属性和方法,vc都能看得见
4549
*/
46-
4750
const School = Vue.extend({
4851
//data中存放组件所需数据
4952
data(){
53+
console.log('@',this)
5054
return {
5155
name:'尚硅谷',
5256
address:'北七家镇-宏福科技园'
5357
}
5458
},
59+
methods: {
60+
showAddress(){
61+
console.log(this.address)
62+
}
63+
},
5564
//template中配置组件的模板结构
5665
template:`
5766
<div>
58-
<h2>学校名:{{name}}</h2>
59-
<h2>学校地址:{{address}}</h2>
67+
<h2 class="title">学校名:{{name}}</h2>
68+
<h2 @click="showAddress">学校地址:{{address}}</h2>
6069
</div>
6170
`
6271
})
63-
Vue.prototype.xyz = 1
72+
6473
//定义个Hello组件
6574
const Hello = Vue.extend({
6675
template:`<h2>Hello</h2>`
6776
})
6877

78+
const App = Vue.extend({
79+
components:{School,Hello},
80+
template:`
81+
<div>
82+
<Hello/>
83+
<School/>
84+
</div>
85+
`
86+
})
87+
6988
//注册School组件----全局注册
7089
// Vue.component('School',School)
7190

7291
//定义一个vm,去管理所有的组件
7392
const vm = new Vue({
7493
el:'#root',
7594
//注册School组件----局部注册----以后工作用的多
76-
components:{
77-
School,
78-
Hello
79-
}
95+
components:{App}
8096
})
8197
</script>
8298
</body>

19_组件_单文件组件/App.vue

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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>

0 commit comments

Comments
 (0)