当前位置:网站首页>atguigu----16-自定义指令
atguigu----16-自定义指令
2022-06-24 06:44:00 【张 邵】
自定义指令总结:
一、定义语法:
(1).局部指令:
new Vue({ new Vue({
directives:{指令名:配置对象} 或 directives{指令名:回调函数}
}) })
(2).全局指令:
Vue.directive(指令名,配置对象) 或 Vue.directive(指令名,回调函数)
二、配置对象中常用的3个回调:
(1).bind:指令与元素成功绑定时调用。
(2).inserted:指令所在元素被插入页面时调用。
(3).update:指令所在模板结构被重新解析时调用。
三、备注:
1.指令定义时不加v-,但使用时要加v-;
2.指令名如果是多个单词,要使用kebab-case命名方式,不要用camelCase命名。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>自定义指令</title>
<script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
<!-- 需求1:定义一个v-big指令,和v-text功能类似,但会把绑定的数值放大10倍。 需求2:定义一个v-fbind指令,和v-bind功能类似,但可以让其所绑定的input元素默认获取焦点。 自定义指令总结: 一、定义语法: (1).局部指令: new Vue({ new Vue({ directives:{指令名:配置对象} 或 directives{指令名:回调函数} }) }) (2).全局指令: Vue.directive(指令名,配置对象) 或 Vue.directive(指令名,回调函数) 二、配置对象中常用的3个回调: (1).bind:指令与元素成功绑定时调用。 (2).inserted:指令所在元素被插入页面时调用。 (3).update:指令所在模板结构被重新解析时调用。 三、备注: 1.指令定义时不加v-,但使用时要加v-; 2.指令名如果是多个单词,要使用kebab-case命名方式,不要用camelCase命名。 -->
<!-- 准备好一个容器-->
<div id="root">
<h2>{
{name}}</h2>
<h2>当前的n值是:<span v-text="n"></span> </h2>
<!-- <h2>放大10倍后的n值是:<span v-big-number="n"></span> </h2> -->
<h2>放大10倍后的n值是:<span v-big="n"></span> </h2>
<button @click="n++">点我n+1</button>
<hr/>
<input type="text" v-fbind:value="n">
</div>
</body>
<script type="text/javascript"> Vue.config.productionTip = false //定义全局指令 /* Vue.directive('fbind',{ //指令与元素成功绑定时(一上来) bind(element,binding){ element.value = binding.value }, //指令所在元素被插入页面时 inserted(element,binding){ element.focus() }, //指令所在的模板被重新解析时 update(element,binding){ element.value = binding.value } }) */ new Vue({
el:'#root', data:{
name:'尚硅谷', n:1 }, directives:{
//big函数何时会被调用?1.指令与元素成功绑定时(一上来)。2.指令所在的模板被重新解析时。 /* 'big-number'(element,binding){ // console.log('big') element.innerText = binding.value * 10 }, */ big(element,binding){
console.log('big',this) //注意此处的this是window // console.log('big') element.innerText = binding.value * 10 }, fbind:{
//指令与元素成功绑定时(一上来) bind(element,binding){
element.value = binding.value }, //指令所在元素被插入页面时 inserted(element,binding){
element.focus() }, //指令所在的模板被重新解析时 update(element,binding){
element.value = binding.value } } } }) </script>
</html>
边栏推荐
- jarvisoj_ level2
- Global and Chinese market of basketball uniforms 2022-2028: Research Report on technology, participants, trends, market size and share
- Combine with (& &) logic or (||), dynamic binding and ternary operation
- Dichotomous special training
- 选择器(>,~,+,[])
- Only two lines are displayed, and the excess part is displayed with Ellipsis
- 简单的折射效果
- 光照使用的简单总结
- [WordPress website] 5 Set code highlight
- C# Lambda
猜你喜欢
![[OGeek2019]babyrop](/img/74/5f93dcee9ea5a562a7fba5c17aab76.png)
[OGeek2019]babyrop

MySQL - three tables (student, course, score) to query the name, number and score of students whose course is mathematics
![[frame rate doubling] development and implementation of FPGA based video frame rate doubling system Verilog](/img/38/92486c92557e6e5a10a362eb2b7bdf.png)
[frame rate doubling] development and implementation of FPGA based video frame rate doubling system Verilog

Win10 build webservice

只显示两行,超出部分省略号显示

jarvisoj_ level2

get_started_3dsctf_2016

More than 60 million shovel excrement officials, can they hold a spring of domestic staple food?

Description of module data serial number positioning area code positioning refers to GBK code

bjdctf_2020_babystack
随机推荐
[WUSTCTF2020]alison_ likes_ jojo
Climbing 10000 NASA pictures about Mars exploration, I found a secret
[WordPress website] 5 Set code highlight
Deploy L2TP in VPN (medium)
(cve-2020-11978) command injection vulnerability recurrence in airflow DAG [vulhub range]
【MySQL 使用秘籍】克隆数据表、保存查询数据至数据表以及创建临时表
Dichotomous special training
20 not to be missed ES6 tips
C# Lambda
How to connect the Bluetooth headset to the computer and how to connect the win10 computer to the Bluetooth headset
湖北专升本-湖师计科
什么是CC攻击?如何判断网站是否被CC攻击? CC攻击怎么防御?
Ultra wideband pulse positioning scheme, UWB precise positioning technology, wireless indoor positioning application
图形技术之坐标转换
阿里云全链路数据治理
[tips] use the deep learning toolbox of MATLAB deepnetworkdesigner to quickly design
[image fusion] image fusion based on directional discrete cosine transform and principal component analysis with matlab code
Global and Chinese market of water massage column 2022-2028: Research Report on technology, participants, trends, market size and share
第三方软件测试公司如何选择?2022国内软件测试机构排名
Tidb operator source code reading (IV) control cycle of components