您好,欢迎访问一九零五行业门户网

一文聊聊Vue中的常用内置指令【大全】

本篇文章对vue所有的内置指令进行回顾总结,前面先说明一些常用指令,不常用的放在后面。
0. 插值表达式说明:插值表达式也叫mustache语法(即双大括号),双大括号标签会被替换为相应组件实例中 msg 属性的值。同时每次 msg 属性更改时它也会同步更新。【相关推荐:vuejs视频教程】
  b2afd68918e546cb374a1c24691a872e    9ef6101249767d81d4698769b8f602fb    c1a436a314ed609750bd7c7d319db4da{{message}} - {{message}}2e9b454fa8428549ca2e64dfac4625cd        2606cfc876c4f7b95f34dead360db33a    c1a436a314ed609750bd7c7d319db4da{{counter * 10}}2e9b454fa8428549ca2e64dfac4625cd    c1a436a314ed609750bd7c7d319db4da{{ message.split( ).reverse().join( ) }}2e9b454fa8428549ca2e64dfac4625cd        ed385a16220899853edae7594c73287b    872d9b32c8da191b77f57bee34da1d47    c1a436a314ed609750bd7c7d319db4da{{getreversemessage()}}2e9b454fa8428549ca2e64dfac4625cd        a9eef1710a66a568c2ef3239283949bc    c1a436a314ed609750bd7c7d319db4da{{ isshow ? 哈哈哈:  }}2e9b454fa8428549ca2e64dfac4625cd    6159835a68da9af36b165079f357645e切换65281c5ac262bf6d81768915a4a77ac0    67ac0dbf43588ec2e5eb6f29e5b49789    4904c3e070de047a8967787da241ac97 赋值语句 -->      21c97d3a051048b8e55e3c8f199a54b2
1. v-on说明: 给元素绑定事件监听器。
缩写:@
参数: event (使用对象语法则为可选项)
修饰符:
.stop ——调用 event.stoppropagation()。.prevent ——调用 event.preventdefault()。.capture ——在捕获模式添加事件监听器。.self ——只有事件从元素本身发出才触发处理函数。.{keyalias} ——只在某些按键下触发处理函数。.once ——最多触发一次处理函数。.left ——只在鼠标左键事件触发处理函数。.right ——只在鼠标右键事件触发处理函数。.middle ——只在鼠标中键事件触发处理函数。.passive ——通过 { passive: true } 附加一个 dom 事件。详细描述:事件类型由参数来指定。表达式可以是一个方法名,一个内联声明,如果有修饰符则可省略。
当用于普通元素,只监听原生 dom 事件。当用于自定义元素组件,则监听子组件触发的自定义事件。当监听原生 dom 事件时,方法接收原生事件作为唯一参数。如果使用内联声明,声明可以访问一个特殊的 $event 变量:v-on:click=handle('ok', $event)。v-on 还支持绑定不带参数的事件/监听器对的对象。请注意,当使用对象语法时,不支持任何修饰符。<!-- 方法处理函数 --><button v-on:click="dothis"></button><!-- 动态事件 --><button v-on:[event]="dothis"></button><!-- 内联声明 --><button v-on:click="dothat('hello', $event)"></button><!-- 缩写 --><button @click="dothis"></button><!-- 使用缩写的动态事件 --><button @[event]="dothis"></button><!-- 停止传播 --><button @click.stop="dothis"></button><!-- 阻止默认事件 --><button @click.prevent="dothis"></button><!-- 不带表达式地阻止默认事件 --><form @submit.prevent></form><!-- 链式调用修饰符 --><button @click.stop.prevent="dothis"></button><!-- 按键用于 keyalias 修饰符--><input @keyup.enter="onenter" /><!-- 点击事件将最多触发一次 --><button v-on:click.once="dothis"></button><!-- 对象语法 --><button v-on="{ mousedown: dothis, mouseup: dothat }"></button>
说明: 动态的绑定一个或多个 attribute,也可以是组件的 prop。
缩写: : 或者 . (当使用 .prop 修饰符)
修饰符:
.camel ——将短横线命名的 attribute 转变为驼峰式命名。.prop ——强制绑定为 dom property。3.2+.attr ——强制绑定为 dom attribute。3.2+用途:
当用于绑定 class 或 style attribute,v-bind 支持额外的值类型如数组或对象。详见下方的指南链接。在处理绑定时,vue 默认会利用 in 操作符来检查该元素上是否定义了和绑定的 key 同名的 dom property。如果存在同名的 property,则 vue 会把作为 dom property 赋值,而不是作为 attribute 设置。这个行为在大多数情况都符合期望的绑定值类型,但是你也可以显式用 .prop 和 .attr 修饰符来强制绑定方式。有时这是必要的,特别是在和自定义元素打交道时。当用于组件 props 绑定时,所绑定的 props 必须在子组件中已被正确声明。当不带参数使用时,可以用于绑定一个包含了多个 attribute 名称-绑定值对的对象。<!-- 绑定 attribute --><img v-bind:src="imagesrc" /><!-- 动态 attribute 名 --><button v-bind:[key]="value"></button><!-- 缩写 --><img :src="imagesrc" /><!-- 缩写形式的动态 attribute 名 --><button :[key]="value"></button><!-- 内联字符串拼接 --><img :src="'/path/to/images/' + filename" /><!-- class 绑定 --><div :class="{ red: isred }"></div><div :class="[classa, classb]"></div><div :class="[classa, { classb: isb, classc: isc }]"></div><!-- style 绑定 --><div :style="{ fontsize: size + 'px' }"></div><div :style="[styleobjecta, styleobjectb]"></div><!-- 绑定对象形式的 attribute --><div v-bind="{ id: someprop, 'other-attr': otherprop }"></div><!-- prop 绑定。“prop” 必须在子组件中已声明。 --><mycomponent :prop="something" /><!-- 传递子父组件共有的 prop --><mycomponent v-bind="$props" /><!-- xlink --><svg><a :xlink:special="foo"></a></svg>
3. v-if说明: 基于表达式值的真假性,来条件性地渲染元素或者模板片段。
<h2 v-if="isshow">哈哈哈哈</h2>
4. v-else说明: 表示 v-if 或 v-if / v-else-if 链式调用的“else 块”。
<h2 v-if="isshow">coder</h2><h2 v-else>bin</h2>
ishow 为 true 显示 coder,反之显示 bin
5. v-else-if说明: 表示 v-if 的“else if 块”。可以进行链式调用。
<template id="my-app"> <input type="text" v-model="score"> <h2 v-if="score > 90">优秀</h2> <h2 v-else-if="score > 60">良好</h2> <h2 v-else>不及格</h2></template>
v-model 后面会说明
6. v-show说明:基于表达式值的真假性,来改变元素的可见性。
详细描述:v-show 通过设置内联样式的 display css 属性来工作,当元素可见时将使用初始 display 值。当条件改变时,也会触发过渡效果。
<template id="my-app"> <h2 v-show="isshow">哈哈哈哈</h2> </template> <script> const app = { template: '#my-app', data() { return { isshow: true } } } vue.createapp(app).mount('#app'); </script>
v-show 不支持在 <template> 元素上使用,也不能和 v-else 搭配使用。
7. v-model说明: 在表单输入元素或组件上创建双向绑定。
仅限: <input>、<select>、 <textarea>、components
修饰符:
.lazy ——监听 change 事件而不是 input.number ——将输入的合法符串转为数字.trim ——移除输入内容两端空格基本使用:
<template id="my-app"> <!-- 1.v-bind value的绑定 2.监听input事件, 更新message的值 --> <!-- <input type="text" :value="message" @input="inputchange"> --> <input type="text" v-model="message"> <h2>{{message}}</h2> </template> <script> const app = { template: '#my-app', data() { return { message: "hello world" } }, methods: { inputchange(event) { this.message = event.target.value; } } } vue.createapp(app).mount('#app');
绑定其他表单:
<template id="my-app"> <!-- 1.绑定textarea --> <label for="intro"> 自我介绍 <textarea name="intro" id="intro" cols="30" rows="10" v-model="intro"></textarea> </label> <h2>intro: {{intro}}</h2> <!-- 2.checkbox --> <!-- 2.1.单选框 --> <label for="agree"> <input id="agree" type="checkbox" v-model="isagree"> 同意协议 </label> <h2>isagree: {{isagree}}</h2> <!-- 2.2.多选框 --> <span>你的爱好: </span> <label for="basketball"> <input id="basketball" type="checkbox" v-model="hobbies" value="basketball"> 篮球 </label> <label for="football"> <input id="football" type="checkbox" v-model="hobbies" value="football"> 足球 </label> <label for="tennis"> <input id="tennis" type="checkbox" v-model="hobbies" value="tennis"> 网球 </label> <h2>hobbies: {{hobbies}}</h2> <!-- 3.radio --> <span>你的爱好: </span> <label for="male"> <input id="male" type="radio" v-model="gender" value="male">男 </label> <label for="female"> <input id="female" type="radio" v-model="gender" value="female">女 </label> <h2>gender: {{gender}}</h2> <!-- 4.select --> <span>喜欢的水果: </span> <select v-model="fruit" multiple size="2"> <option value="apple">苹果</option> <option value="orange">橘子</option> <option value="banana">香蕉</option> </select> <h2>fruit: {{fruit}}</h2> </template> <script> const app = { template: '#my-app', data() { return { intro: "hello world", isagree: false, hobbies: ["basketball"], gender: "", fruit: "orange" } }, methods: { commitform() { axios } } } vue.createapp(app).mount('#app'); </script>
v-model修饰符的使用
<template id="my-app"> <!-- 1.lazy修饰符 --> <!-- <input type="text" v-model.lazy="message"> --> <!-- 2.number修饰符 --> <!-- <input type="text" v-model.number="message"> <h2>{{message}}</h2> <button @click="showtype">查看类型</button> --> <!-- 3.trim修饰符 --> <input type="text" v-model.trim="message"> <button @click="showresult">查看结果</button> </template> <script> const app = { template: '#my-app', data() { return { message: "hello world" } }, methods: { showtype() { console.log(this.message, typeof this.message); }, showresult() { console.log(this.message); } } } vue.createapp(app).mount('#app'); </script>
8. v-for说明: 基于原始数据多次渲染元素或模板块。
详细描述:
指令值必须使用特殊语法 alias in expression 为正在迭代的元素提供一个别名:
<div v-for="item in items"> {{ item.text }}</div>
或者,你也可以为索引指定别名 (如果用在对象,则是键值):
<div v-for="(item, index) in items"></div><div v-for="(value, key) in object"></div><div v-for="(value, name, index) in object"></div>
v-for 的默认方式是尝试就地更新元素而不移动它们。要强制其重新排序元素,你需要用特殊 attribute key 来提供一个排序提示:
<div v-for="item in items" :key="item.id"> {{ item.text }}</div>
9. v-slot说明: 用于声明具名插槽或是期望接收 props 的作用域插槽。
缩写: #
参数:插槽名 (可选,默认是 default)
仅限:
<template>components (用于带有 prop 的单个默认插槽)示例
<!-- 具名插槽 --><baselayout> <template v-slot:header> header content </template> <template v-slot:default> default slot content </template> <template v-slot:footer> footer content </template></baselayout><!-- 接收 prop 的具名插槽 --><infinitescroll> <template v-slot:item="slotprops"> <div class="item"> {{ slotprops.item.text }} </div> </template></infinitescroll><!-- 接收 prop 的默认插槽,并解构 --><mouse v-slot="{ x, y }"> mouse position: {{ x }}, {{ y }}</mouse>
10. v-text说明: 更新元素的文本内容。
详细描述: v-text 通过设置元素的 textcontent 属性来工作,因此它将覆盖元素中所有现有的内容。如果你需要更新 textcontent 的部分,应该使用 mustache interpolations 代替。
<span v-text="msg"></span><!-- 等同于 --><span>{{msg}}</span>
11. v-html说明: 更新元素的 innerhtml。
详细描述: v-html 的内容直接作为普通 html 插入—— vue 模板语法是不会被解析的。如果你发现自己正打算用 v-html 来编写模板,不如重新想想怎么使用组件来代替。
安全说明:在你的站点上动态渲染任意的 html 是非常危险的,因为它很容易导致 xss 攻击。请只对可信内容使用 html 插值,绝不要将用户提供的内容作为插值
<div v-html="html"></div>
12. v-pre说明: 跳过该元素及其所有子元素的编译。
详细描述:元素内具有 v-pre,所有 vue 模板语法都会被保留并按原样渲染。最常见的用例就是显示原始双大括号标签及内容。
<span v-pre>{{ this will not be compiled }}</span>
13. v-once说明: 跳过该元素及其所有子元素的编译。
详细描述: 在随后的重新渲染,元素/组件及其所有子项将被当作静态内容并跳过渲染。这可以用来优化更新时的性能。
<!-- 单个元素 --><span v-once>this will never change: {{msg}}</span><!-- 带有子元素的元素 --><div v-once> <h1>comment</h1> <p>{{msg}}</p></div><!-- 组件 --><mycomponent v-once :comment="msg" /><!-- `v-for` 指令 --><ul> <li v-for="i in list" v-once>{{i}}</li></ul>
14. v-cloak说明: 用于隐藏尚未完成编译的 dom 模板。
详细描述:该指令只在没有构建步骤的环境下需要使用。
当使用直接在 dom 中书写的模板时,可能会出现一种叫做“未编译模板闪现”的情况:用户可能先看到的是还没编译完成的双大括号标签,直到挂载的组件将它们替换为实际渲染的内容。v-cloak 会保留在所绑定的元素上,直到相关组件实例被挂载后才移除。配合像 [v-cloak] { display: none } 这样的 css 规则,它可以在组件编译完毕前隐藏原始模板。[v-cloak] { display: none;}
<div v-cloak> {{ message }}</div>
更多编程相关知识,请访问:编程入门!!
以上就是一文聊聊vue中的常用内置指令【大全】的详细内容。
其它类似信息

推荐信息