vue 内置动态组件component 使⽤详解
1 动态组件介绍
component 是vue内置组件,主要作⽤为动态渲染组件,基本⽤法如下:其中,根据绑定的is 的值决定拿个组件被渲染。
2 使⽤整洁的英文
2.1 动态组件切换
通过动态组件的属性,除了基本的组件动态渲染以外,我们常将其⽤作与动态组件的切换,⽰例如下:
代码如下:<!-- 动态组件由 vm 实例的 `componentName` property 控制 --><component :is ="componentName"></component >
1
2
3<template > <div > <title -bar :title ="title" @goBack ="goback"></title -bar > <component :is ="sh
owComponent"></component > <div id ="dynamic-component-demo" class ="demo"> <button v -for ="tab in tabs" :key ="tab" :class ="['tab-button', { active: currentTab === tab }]" @click ="currentTab = tab" > {{ tab }} </button > <component :is ="currentTabComponent" class ="tab" :data ="propsData" :dataA ="propsDataA" :dataB ="propsDataB" ></component > </div > </div ></template ><script >import TitleBar from "@/components/TitleBar";import comA from "./components/comA";import comB from "./components/comB";export default { name : "", components : { TitleBar , comA , comB }, data () { return { title : "动态组件", showComponent : "", // currentTabComponent: '' currentTab : "comA", tabs : ["comA", "comB"], propsData : "nd data", propsDataA : "comA nd data",
1
2
3
4
5
6
7
8
9
10
11
12
13
14
believeyourlf15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
propsDataA : "comA nd data", propsDataB : "comB nd data" }; }, created () { tTimeout (() => { this .showComponent = "comA"; this .changeCom (); }, 1000); }, computed : { currentTabComponent () { return this .currentTab ; // return "tab-" + LowerCa(); } }, methods : { changeCom () { tTimeout (() => { this .showComponent = "comB"; }, 2000); }, // 返回⽅法 goback () { // this.$emit("GoBack"); } }};</script ><style scoped >#page -title { width : 100%; background -color : #fff ; display : flex ; justify -content : center ;}.backImg { width : 20px ;}.demo { font -family : sans -rif ; border : 1px solid #eee ; border -radius : 2px ; padding : 20px 30px ; margin -top : 1em ; margin -bottom : 40px ;
ur -lect : none ; overflow -x : auto ;}.tab -button { padding : 6px 10px ; border -top -left -radius : 3px ; border -top -right -radius : 3px ; border : 1px solid #ccc ; cursor : pointer ; background : #f0f0f0; margin -bottom : -1px ; margin -right : -1px ;}.tab -button :hover { background : #e0e0e0;}.tab -button .active { background : #e0e0e0;}
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
八下英语单词表90
91
92
93
94
stay here forever歌词
95
96
97
98
99
100
101
102
103
104
105
bubblegum106
107
108
109
asfd2.2 动态组件传值动态组件的传值遵循基本组件传值规则,除了⽀持v-bind 传值以外,还⽀持ref 引⽤传值;使⽤引⽤传值需要注意的是,需要确定组件之后,再使⽤ref属性进⾏传值,否则将会⽆法获取应⽤组件的属性。
2.3 动态组件数据缓存
在2.1中,若是数据需要动态渲染,组件切换之后会导致之前获得的数据丢失,这个时候,若我们想要在组件切换过程中保持这些组件的状态,以避免重复渲染导致性能问题,则可以在动态组件上使⽤keep-alive 来缓存组件中的数据,代码如下所⽰:这样⼀来,我们就可以实现多个组件切换时缓存组件数据的效果了;更多关于keep-alive 的使⽤⽅法详见:}.demo -tab { border : 1px solid #ccc ; padding : 10px ;}.tab { margin : 20px auto ;}</style >
109
110
111
112
113
114西藏景点
115
116
117
118<template > <div > <title-bar :title ="title " @goBack ="goback "></title-bar > <component :is ="showComponent "></component > <div id ="dynamic-component-demo " class ="demo "> <button v-for ="tab in tabs " :key ="tab " :class ="['tab-button ', { active: currentTab === tab }]" @click ="currentTab = tab " > {{ tab }} </button > <keep-alive > <component :is ="currentTabComponent " class ="tab " ></component > </keep-alive > </div > </div ></template >
1
2
3
4
5
6
7
8
9
undertaker
10
11
12
13
14
15
16
17
mba面试报名
18
造价员考试19
20
21
22
23