vue-component

接下来,表演默写代码。

全局组件

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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
<backend></backend>
</div>
<div id="app2">
<backend></backend>
</div>
<script>
<!-- 此处定义的是全局组件 -->
Vue.component('backend', {
template: '<div style="background-color:yellow">This is Backend</div>'
});
new Vue({
el: '#app'
});
<!-- 所有的el都能使用全局组件 -->
new Vue({
el: '#app2'
});
</script>
</body>
</html>

局部组件

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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
<background></background>
</div>

<!-- id='app2' 无法获取到 id='app'中定义的局部component -->
<div id="app2">
<background></background>
</div>
<script>
new Vue({
el: '#app',
components: {
background: {
template:
'<div style="background-color:yellow">This is Backend</div>'
}
}
});

new Vue({
el: '#app2'
});
</script>
</body>
</html>
  • 组件的定义,是 ,多看才能熟悉啊!😓
Author: Chandler Kwok
Link: http://yoursite.com/2020/03/23/vue-component/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.