@@ -22,7 +22,7 @@ Vue内置的按键修饰符:
22
22
```
23
23
24
24
25
- 比如说,` keyup ` 指的是,键盘抬起时的监听事件 。` .enter ` 指的是按enter键的按键修饰符 。
25
+ 比如说,` keyup ` 指的是:键盘(任何键位)抬起时的监听事件 。` .enter ` 指的是:按enter键的按键修饰符。我们把这两个结合起来看看 。
26
26
27
27
** ` @keyup.enter ` 举例** :按enter键后的监听事件
28
28
@@ -36,7 +36,7 @@ Vue内置的按键修饰符:
36
36
<input type =" text" v-model =" formData.name" @keyup.enter =" addData" >
37
37
```
38
38
39
- 注意,如果写成` @keyup="addData" ` ,效果却是:只要键盘打了字 (还没来得及按enter键),就会执行addData()方法,这种效果显然不是我们想要的。
39
+ 注意,如果写成` @keyup="addData" ` ,效果却是:只要键盘的任何键位打了字 (还没来得及按enter键),就会执行addData()方法,这种效果显然不是我们想要的。所以要加上修饰符 ` .enter ` ,表示只针对enter键 。
40
40
41
41
### 自定义的按键修饰符
42
42
@@ -65,13 +65,13 @@ Vue内置的按键修饰符:
65
65
66
66
然后,我们就可以使用键盘码的别名了。
67
67
68
- ## 自定义指令
68
+ ## 自定义全局指令
69
69
70
- ### 自定义全局指令的举例
70
+ ### 自定义全局指令的举例1
71
71
72
- ** 举例 ** :网页打开后, 让指定文本框自动获取焦点。
72
+ ** 举例1 ** :让指定文本框自动获取焦点
73
73
74
- 如果我们想实现上面的例子 ,原生js的写法是:
74
+ 如果我们想实现这个例子 ,原生js的写法是:
75
75
76
76
``` javascript
77
77
// 原生js写法:网页一打开,就让指定的输入框自动获取焦点
@@ -84,16 +84,16 @@ Vue内置的按键修饰符:
84
84
85
85
但我们不建议这样做。我们可以通过Vue中的自定义指令来实现这个例子。步骤如下。
86
86
87
-
88
87
(1)使用` Vue.directive() ` 自定义全局指令:
89
88
90
89
``` javascript
91
90
91
+ // 自定义全局指令 v-focus:让文本框自动获取焦点
92
92
// 参数1:指令的名称。注意,在定义的时候,指令的名称前面,不需要加 v- 前缀;但是:在`调用`的时候,必须在指令名称前 加上 v- 前缀
93
93
// 参数2:是一个对象,这个对象身上,有一些指令相关的函数,这些函数可以在特定的阶段,执行相关的操作
94
94
Vue .directive (' focus' , {
95
95
// 在每个函数中,第一个参数,永远是 el ,表示 被绑定了指令的那个元素,这个 el 参数,是一个原生的JS对象(DOM对象)
96
- bind : function (el ) { // 每当指令绑定到元素上的时候,会立即执行这个 bind 函数,只执行一次
96
+ bind : function (el ) { // 每当指令绑定到元素上的时候,会立即执行这个 bind 函数,【 只执行一次】
97
97
// 在元素 刚绑定了指令的时候,还没有 插入到 DOM中去,这时候,调用 focus 方法没有作用
98
98
// 因为,一个元素,只有插入DOM之后,才能获取焦点
99
99
// el.focus()
@@ -102,20 +102,274 @@ Vue内置的按键修饰符:
102
102
el .focus ()
103
103
// 和JS行为有关的操作,最好在 inserted 中去执行,放置 JS行为不生效
104
104
},
105
- updated : function (el ) { // 当VNode更新的时候,会执行 updated, 可能会触发多次
105
+ updated : function (el ) { // 当VNode更新的时候,会执行 updated, 【 可能会触发多次】
106
106
107
107
}
108
108
})
109
109
```
110
110
111
+ 上方的代码中,如果我们把` el.focus() ` 这行代码写在` bind ` 方法里,是没有效果的(但不会报错)。没有效果是因为,在执行到` bind ` 方法的时候,元素还没有插入到dom中去。
111
112
112
- 上方的代码中,
113
+ 由此可以看看出: ` bind ` 、 ` inserted ` 、 ` updated ` 这三个钩子函数的执行时机不同,且执行的次数有区别。
113
114
114
115
(2)在指定的文本框上加``:
115
116
116
117
``` html
117
118
<input type =" text" id =" search" v-model =" keywords" v-focus >
118
119
```
119
120
121
+ 完整版代码如下:
122
+
123
+ ``` html
124
+ <!DOCTYPE html>
125
+ <html lang =" en" >
126
+
127
+ <head >
128
+ <meta charset =" UTF-8" >
129
+ <meta name =" viewport" content =" width=device-width, initial-scale=1.0" >
130
+ <meta http-equiv =" X-UA-Compatible" content =" ie=edge" >
131
+ <title >Document</title >
132
+ <script src =" vue2.5.16.js" ></script >
133
+ </head >
134
+
135
+ <body >
136
+
137
+ <div id =" app" >
138
+ 搜索框:
139
+ <input type =" text" id =" search" v-model =" name" v-focus >
140
+ </div >
141
+
142
+ <script >
143
+
144
+ // 自定义全局指令 v-focus,让文本框自动获取焦点
145
+ // 参数1:指令的名称。注意,在定义的时候,指令的名称前面,不需要加 v- 前缀;但是:在`调用`的时候,必须在指令名称前 加上 v- 前缀
146
+ // 参数2:是一个对象,这个对象身上,有一些指令相关的函数,这些函数可以在特定的阶段,执行相关的操作
147
+ Vue .directive (' focus' , {
148
+ // 在每个函数中,第一个参数,永远是 el ,表示 被绑定了指令的那个元素,这个 el 参数,是一个原生的JS对象(DOM对象)
149
+ bind : function (el ) { // 每当指令绑定到元素上的时候,会立即执行这个 bind 函数,【只执行一次】
150
+ // 在元素 刚绑定了指令的时候,还没有 插入到 DOM中去,这时候,调用 focus 方法没有作用
151
+ // 因为,一个元素,只有插入DOM之后,才能获取焦点
152
+ // el.focus()
153
+ },
154
+ inserted : function (el ) { // inserted 表示元素 插入到DOM中的时候,会执行 inserted 函数【触发1次】
155
+ el .focus ()
156
+ // 和JS行为有关的操作,最好在 inserted 中去执行,防止 JS行为不生效
157
+ },
158
+ updated : function (el ) { // 当VNode更新的时候,会执行 updated, 【可能会触发多次】
159
+ }
160
+ })
161
+
162
+ new Vue ({
163
+ el: ' #app' ,
164
+ data: {
165
+ name: ' smyhvae'
166
+ }
167
+ })
168
+ </script >
169
+ </body >
170
+
171
+ </html >
172
+
173
+ ```
174
+
175
+
176
+ ### 自定义全局指令:使用钩子函数的第二个binding参数拿到传递的值
177
+
178
+ ** 举例2** :设置DOM元素的color样式
179
+
180
+
181
+ 参考举例1中的写法,我们可能会这样给DOM元素设置样式:
182
+
183
+ ``` html
184
+ <!DOCTYPE html>
185
+ <html lang =" en" >
186
+
187
+ <head >
188
+ <meta charset =" UTF-8" >
189
+ <meta name =" viewport" content =" width=device-width, initial-scale=1.0" >
190
+ <meta http-equiv =" X-UA-Compatible" content =" ie=edge" >
191
+ <title >Document</title >
192
+ <script src =" vue2.5.16.js" ></script >
193
+ </head >
194
+
195
+ <body >
196
+
197
+ <div id =" app" >
198
+ 搜索框:
199
+ <input type =" text" id =" search" v-model =" name" v-color >
200
+ </div >
201
+
202
+ <script >
203
+
204
+ // 自定义全局指令 v-color:设置DOM元素的color属性
205
+ Vue .directive (' color' , {
206
+ bind : function (el ) { // 每当指令绑定到元素上的时候,会立即执行这个 bind 函数,【只执行一次】
207
+ el .style .color = ' red' ;
208
+ },
209
+ inserted : function (el ) { // inserted 表示元素 插入到DOM中的时候,会执行 inserted 函数【触发1次】
210
+ // 和JS行为有关的操作,最好在 inserted 中去执行,防止 JS行为不生效
211
+ // el.focus()
212
+ },
213
+ updated : function (el ) { // 当VNode更新的时候,会执行 updated, 【可能会触发多次】
214
+ }
215
+ })
216
+
217
+ new Vue ({
218
+ el: ' #app' ,
219
+ data: {
220
+ name: ' '
221
+ }
222
+ })
223
+ </script >
224
+ </body >
225
+
226
+ </html >
227
+ ```
228
+
229
+ 如上方代码所示,我们自定义了一个指令` v-color ` ,然后在` input ` 标签中用上了这个指令,就给元素设置了color属性。但是这个代码有个弊端是:color的属性值在定义指令的时候,被写死了。如何完善呢?我们可以在DOM元素中传参。一起来看看。
230
+
231
+ 代码如下:【荐】
232
+
233
+ ``` html
234
+ <!DOCTYPE html>
235
+ <html lang =" en" >
236
+
237
+ <head >
238
+ <meta charset =" UTF-8" >
239
+ <meta name =" viewport" content =" width=device-width, initial-scale=1.0" >
240
+ <meta http-equiv =" X-UA-Compatible" content =" ie=edge" >
241
+ <title >Document</title >
242
+ <script src =" vue2.5.16.js" ></script >
243
+ </head >
244
+
245
+ <body >
246
+
247
+ <div id =" app" >
248
+ 搜索框1:
249
+ <input type =" text" id =" search" v-model =" name" v-color =" 'green'" >
250
+ </div >
251
+
252
+ <script >
253
+
254
+ // 自定义全局指令 v-color:设置DOM元素的color属性
255
+ Vue .directive (' color' , {
256
+ // 样式,只要通过指令绑定给了元素,不管这个元素有没有被插入到页面中去,这个元素肯定有了一个内联的样式
257
+ // 将来元素肯定会显示到页面中,这时候,浏览器的渲染引擎必然会解析样式,应用给这个元素
258
+ // 意思是说,我们可以把样式的代码写到bind中去(即使这个时候,dom元素还没有被创建)
259
+ bind : function (el , binding ) { // 每当指令绑定到元素上的时候,会立即执行这个 bind 函数,【只执行一次】
260
+
261
+ console .log (binding .name ); // 打印结果:color
262
+ console .log (binding .value ); // 打印结果:green
263
+ console .log (binding .expression ); // 'green'
264
+
265
+ el .style .color = binding .value // 通过bining拿到v-color中传递过来的值
266
+
267
+ },
268
+ inserted : function (el ) { // inserted 表示元素 插入到DOM中的时候,会执行 inserted 函数【触发1次】
269
+ // 和JS行为有关的操作,最好在 inserted 中去执行,防止 JS行为不生效
270
+ // el.focus()
271
+ },
272
+ updated : function (el ) { // 当VNode更新的时候,会执行 updated, 【可能会触发多次】
273
+ }
274
+ })
275
+
276
+ new Vue ({
277
+ el: ' #app' ,
278
+ data: {
279
+ name: ' smyhvae'
280
+ }
281
+ })
282
+ </script >
283
+ </body >
284
+
285
+ </html >
286
+ ```
287
+
288
+ 上方代码中,bind方法里传递的第二个参数` binding ` ,可以拿到DOM元素中` v-color ` 里填的值。注意,` v-color="'green'" ` ,这里面写的是字符串常量;如果去掉单引号,就成了变量,不是我们想要的。
289
+
290
+ 效果:
291
+
292
+ 20180610_1323.png
293
+
294
+ ** 自定义全局指令的简写形式** :
295
+
296
+
297
+ 在很多时候,你可能想在 bind 和 update 时触发相同行为,而不关心其它的钩子。比如上面的代码中,我们可以写成简写形式:
298
+
299
+ ``` javascript
300
+ Vue .directive (' color' , function (el , binding ) { // 注意,这个function等同于把代码写到了 bind 和 update 中去
301
+ el .style .color = binding .value
302
+ })
303
+ ```
304
+
305
+
306
+ ## 自定义私有指令
307
+
308
+ ** 自定义私有指令** :在某一个 vue 对象内部自定义的指令称之为私有指令。这种指令只有在当前vue对象的el指定的监管区域有用。
309
+
310
+ 代码举例:(设置文字的font-weight属性)
311
+
312
+ ``` html
313
+ <!DOCTYPE html>
314
+ <html lang =" en" >
315
+
316
+ <head >
317
+ <meta charset =" UTF-8" >
318
+ <meta name =" viewport" content =" width=device-width, initial-scale=1.0" >
319
+ <meta http-equiv =" X-UA-Compatible" content =" ie=edge" >
320
+ <title >Document</title >
321
+ <script src =" vue2.5.16.js" ></script >
322
+ </head >
323
+
324
+ <body >
325
+
326
+ <div id =" app" >
327
+ <span v-fontweight =" 600" >生命壹号</span >
328
+ </div >
329
+ <script >
330
+
331
+ new Vue ({
332
+ el: ' #app' ,
333
+ data: {
334
+ name: ' smyhvae'
335
+ },
336
+ // 自定义私有指令
337
+ directives: {
338
+ ' fontweight' : {
339
+ bind : function (el , binding ) {
340
+ el .style .fontWeight = binding .value ;
341
+ }
342
+ }
343
+ }
344
+ })
345
+ </script >
346
+ </body >
347
+
348
+ </html >
349
+ ```
350
+
351
+ 效果:
352
+
353
+ 20180610_1400.png
354
+
355
+ 注意, el.style.fontWeight设置属性值,至少要600,否则看不到加粗的效果。
356
+
357
+ ** 自定义私有指令的简写形式** :
358
+
359
+ 在很多时候,你可能想在 bind 和 update 时触发相同行为,而不关心其它的钩子。比如上面的代码中,我们可以写成简写形式:
360
+
361
+ ```
362
+ //自定义私有指令(简写形式)
363
+ directives: {
364
+ 'fontweight': function (el, binding) { //注意,这个function等同于把代码写到了 bind 和 update 中去
365
+ el.style.fontWeight = binding.value;
366
+ }
367
+ }
368
+ ```
369
+
370
+
371
+
372
+
373
+
120
374
121
375
0 commit comments