File tree Expand file tree Collapse file tree 2 files changed +115
-0
lines changed Expand file tree Collapse file tree 2 files changed +115
-0
lines changed Original file line number Diff line number Diff line change 4
4
5
5
组件 (Component) 是 Vue.js 最强大的功能之一。组件可以扩展 HTML 元素,** 封装可重用的代码** 。在较高层面上,组件是自定义元素,Vue.js 的编译器为它添加特殊功能。在有些情况下,组件也可以表现为用 is 特性进行了扩展的原生 HTML 元素,是在前端在单页面应用(SPA)上最好的一种实现方式,把所有** 功能模块拆解成单独的组件** ,每个组件都有** 独立的作用域** ,且还** 可以相互通信** 。
6
6
7
+ ![ img1.png] ( https://i.loli.net/2020/08/17/GvKlogTcXY67C8j.png )
8
+
7
9
## 认识单页面应用(SPA)
8
10
9
11
在传统的页面之间跳转,是通过刷新,重新渲染一个页面而实现,在渲染的过程中势必要加载外部资源文件,页面在服务器中渲染出来是通过一系列的生命周期,在这个过程中会因为网速等硬件问题直接影响页面的加载速度,为解决这一问题,前端在新的设计模式上引入了组件的概念,页面之间的跳转变成了组件之间的切换,不需要重新加载整个页面,也不用考虑页面的生命周期,换成组件的生命周期,在性能上大大的提升了。
@@ -173,3 +175,68 @@ var alone = new Vue({
173
175
<button >0</button >
174
176
</div >
175
177
```
178
+
179
+ ---
180
+
181
+ ## 特殊的 HTML 标签中需要使用 is
182
+
183
+ 像 ` <ul> ` 、` <ol> ` 、` <table> ` 、` <select> ` 这样的元素里允许包含的元素有限制,而另一些像 ` <option> ` 这样的元素只能出现在某些特定元素的内部。
184
+
185
+ ``` html
186
+ <div id =" special" >
187
+ <select >
188
+ <!-- 错误写法如下 -->
189
+
190
+ <!--
191
+ <privateOption />
192
+ 会被当作无效的东西过滤掉
193
+ -->
194
+
195
+ <!-- 正确写法 -->
196
+ <option is =" privateOption" ></option >
197
+ </select >
198
+ </div >
199
+ ```
200
+
201
+ ---
202
+
203
+ ## 动态组件 - : is
204
+
205
+ 某些场景下我们需要动态的切换不同的页面这时候,讲一个个需求封装成一个个组件,用动态组件就可以实现
206
+
207
+ ``` html
208
+ <div id =" dynamic" >
209
+ <button @click =" change" >{{title}}</button >
210
+ <br />
211
+ <p :is =" title" ></p >
212
+ </div >
213
+ ```
214
+
215
+ ``` js
216
+ // 点击后根据变量的改变加载不同的组件
217
+ var dynamic = new Vue ({
218
+ el: " #dynamic" ,
219
+ data: {
220
+ title: " red" ,
221
+ },
222
+ methods: {
223
+ change () {
224
+ this .title = this .title == " red" ? " green" : " red" ;
225
+ },
226
+ },
227
+ components: {
228
+ red: {
229
+ template: " <h1>Red</h1>" ,
230
+ },
231
+ green: {
232
+ template: " <h2>Green</h2>" ,
233
+ },
234
+ },
235
+ });
236
+ ```
237
+
238
+ ---
239
+
240
+ ## Prop 实现数据通信
241
+
242
+ 在 Vue 中,父子组件的关系可以总结为 ` prop ` 向下传递,事件向上传递。父组件通过 ` prop ` 给子组件下发数据,子组件通过` 事件 ` 给父组件发送消息。
Original file line number Diff line number Diff line change 25
25
< p > {{count}}</ p >
26
26
< my-component />
27
27
</ div >
28
+ < br />
29
+
30
+ <!-- 特殊的HTML结构中需要使用到is -->
31
+ < div id ="special ">
32
+ < select >
33
+ <!-- <privateOption /> -->
34
+ < option is ="privateOption "> </ option >
35
+ </ select >
36
+ </ div >
37
+ < br />
38
+
39
+ <!-- 动态组件 -->
40
+ < div id ="dynamic ">
41
+ < button @click ="change "> {{title}}</ button >
42
+ < br />
43
+ < p :is ="title "> </ p >
44
+ </ div >
28
45
</ body >
29
46
</ html >
30
47
< script src ="https://cdn.bootcdn.net/ajax/libs/vue/2.6.1/vue.js "> </ script >
101
118
} ,
102
119
} ,
103
120
} ) ;
121
+
122
+ //特殊的HTML需要使用到is
123
+ var special = new Vue ( {
124
+ el : "#special" ,
125
+ components : {
126
+ privateOption : {
127
+ template : "<option>我是特殊的HTML</option>" ,
128
+ } ,
129
+ } ,
130
+ } ) ;
131
+
132
+ //动态组件
133
+ var dynamic = new Vue ( {
134
+ el : "#dynamic" ,
135
+ data : {
136
+ title : "red" ,
137
+ } ,
138
+ methods : {
139
+ change ( ) {
140
+ this . title = this . title == "red" ? "green" : "red" ;
141
+ } ,
142
+ } ,
143
+ components : {
144
+ red : {
145
+ template : "<h1>Red</h1>" ,
146
+ } ,
147
+ green : {
148
+ template : "<h2>Green</h2>" ,
149
+ } ,
150
+ } ,
151
+ } ) ;
104
152
</ script >
You can’t perform that action at this time.
0 commit comments