下面看下vue component動態組件(詳情看vue.js官網動態組件)
動態組件
通過component標簽 的is屬性來進行組件的切換
is的屬性值決定要顯示的組件,所以將is的屬性值設置為data中的值,以便於動態變化
1
2
3
4
5
6
7
|
<template>
<div class=
"app"
>
<component :is=
"組件名稱"
>
</component>
</div>
</template>
|
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
|
<!DOCTYPE html>
<html lang=
"en"
>
<head>
<meta charset=
"UTF-8"
>
<title>動態組件</title>
<script src=
"bower_components/vue/dist/vue.js"
></script>
<style>
</style>
</head>
<body>
<div id=
"box"
>
<input type=
"button"
@click=
"a='aaa'"
value=
"aaa組件"
>
<input type=
"button"
@click=
"a='bbb'"
value=
"bbb組件"
>
<component :is=
"a"
></component> <!-- 動態組件-->
</div>
<script>
var
vm=
new
Vue({
el:
'#box'
,
data:{
a:
'aaa'
},
components:{
'aaa'
:{
template:
'<h2>我是aaa組件</h2>'
},
'bbb'
:{
template:
'<h2>我是bbb組件</h2>'
}
}
});
</script>
</body>
</html>
|