了解vue.js中的常用指令(总结)

互联网 20-11-6

v-text

v-text主要用来更新textContent,可以等同于JS的text属性。

<span v-text="text"></span> // 等同于下方语句:  <span>{{text}}</span>

v-html

双大括号的方式会将数据解释为纯文本,而非HTML。为了输出真正的HTML,可以用v-html指令。它等同于JS的 innerHtml 属性。

注意:内容按普通 HTML 插入 - 不会作为 Vue 模板进行编译 。

<div v-html="html"></div>

v-show

等同于 css 的 dispaly 属性切换 “none” 和 “block” 设值。

<div v-show="isShow">hello world</div>

v-if

v-if可以实现条件渲染,Vue会根据表达式的值的真假条件来渲染元素。

<div v-show="isShow">hello world</div>

上方代码,如果 isShow 为 false 则div被渲染,否则不被渲染。

注意:

v-if 需要和 v-show 区分开,v-show 的元素会始终被渲染并保存在 dom 中,它只是简单的切换 css 的 dispaly 属性。

v-if有更高的切换开销。

v-show有更高的初始渲染开销。

所以,如果要非常频繁的切换,使用 v-show 较好;如果在运行时条件不太可能改变,使用 v-if 较好。

v-else

<div v-if="isSow">值为true的时候显示的内容</div> <div v-else>值为false的时候显示的内容</div>

v-else-if

v-else-if充当v-if的else-if块,可以链式的使用多次。 类似JS的 if .. else if .. else

<div v-if="type==='A'">   A </div> <div v-if="type==='B'">   B </div> <div v-if="type==='C'">   C </div> <div v-else>   Not A,B,C </div>

v-for

用v-for指令根据遍历数组来进行渲染。

<ul> 	<li v-for="item in items">{{item.name}}</li> </ul>  <script> new Vue({   el: '#app',   data: {     items: [       { name: 'Runoob' },       { name: 'Google' },       { name: 'Taobao' }     ]   } }) </script>  // 补充: // 也可以自行指定参数,最多可以接受3个参数 <div v-for="(item, index) in items"></div> <div v-for="(val, key) in object"></div> <div v-for="(val, name, index) in object"></div>  // 迭代对象 <ul>     <li v-for="value in object">      {{ index }}. {{ key }} : {{ value }} </li>  // 迭代整数 <ul>     <li v-for="n in 10">      {{ n }}     </li> </ul>

v-on

v-on="show" 可以简写为: @show

<!-- 方法处理器 --> <button v-on:click="doThis"></button>  <!-- 动态事件 (2.6.0+) --> <button v-on:[event]="doThis"></button>  <!-- 内联语句 --> <button v-on:click="doThat('hello', $event)"></button>  <!-- 缩写 --> <button @click="doThis"></button>  <!-- 动态事件缩写 (2.6.0+) --> <button @[event]="doThis"></button>  <!-- 停止冒泡 --> <button @click.stop="doThis"></button>  <!-- 阻止默认行为 --> <button @click.prevent="doThis"></button>  <!-- 阻止默认行为,没有表达式 --> <form @submit.prevent></form>  <!--  串联修饰符 --> <button @click.stop.prevent="doThis"></button>  <!-- 键修饰符,键别名 --> <input @keyup.enter="onEnter">  <!-- 键修饰符,键代码 --> <input @keyup.13="onEnter">  <!-- 点击回调只会触发一次 --> <button v-on:click.once="doThis"></button>  <!-- 对象语法 (2.4.0+) --> <button v-on="{ mousedown: doThis, mouseup: doThat }"></button>

还可以使用修饰符,具体如下:

.stop - 调用 event.stopPropagation()。

.prevent - 调用 event.preventDefault()。

.capture - 添加事件侦听器时使用 capture 模式。

.self - 只当事件是从侦听器绑定的元素本身触发时才触发回调。

.{keyCode | keyAlias} - 只当事件是从特定键触发时才触发回调。

.native - 监听组件根元素的原生事件。

.once - 只触发一次回调。

.left - 只当点击鼠标左键时触发。

.right - 只当点击鼠标右键时触发。

.middle - 只当点击鼠标中键时触发。

.passive - 以 { passive: true } 模式添加侦听器

v-bind

动态地绑定一个或多个特性,或一个组件 prop 到表达式。常用于动态绑定class和style。以及href等。

可简写为:" : ",如:

v-bind:class=" isActive : 'active' :' ' ",可简写为::class=" isActive : 'active' :' ' "

<div v-bind:class=" isActive : 'active' :' ' "></div> <script>   var app = new Vue({     el: '#app',     data: {       isActive : true,      }   }) </script>  //渲染结果为: <div class="active"></div>

绑定多个 class ,具体如下:

<div v-bind:class="[ isActive : 'active' :' ' , isError: 'error' :' ' ]"> </div> <script>   var app = new Vue({     el: '#app',     data: {       isActive : true,        isError:  true,     }   }) </script>  //渲染结果为: <div class="active error"></div>

其他实例,具体见下方代码:

<!-- 绑定一个属性 --> <img v-bind:src="imageSrc">  <!-- 动态特性名 (2.6.0+) --> <button v-bind:[key]="value"></button>  <!-- 缩写 --> <img :src="imageSrc">  <!-- 动态特性名缩写 (2.6.0+) --> <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 }]">  <!-- style 绑定 --> <div :style="{ fontSize: size + 'px' }"></div> <div :style="[styleObjectA, styleObjectB]"></div>  <!-- 绑定一个有属性的对象 --> <div v-bind="{ id: someProp, 'other-attr': otherProp }"></div>  <!-- 通过 prop 修饰符绑定 DOM 属性 --> <div v-bind:text-content.prop="text"></div>  <!-- prop 绑定。“prop”必须在 my-component 中声明。--> <my-component :prop="someThing"></my-component>  <!-- 通过 $props 将父组件的 props 一起传给子组件 --> <child-component v-bind="$props"></child-component>  <!-- XLink --> <svg><a :xlink:special="foo"></a></svg>

v-model

<div id="app">   <input v-model="somebody">   <p>hello {{somebody}}</p> </div> <script>   var app = new Vue({     el: '#app',     data: {       somebody:'小明'     }   }) </script>

这个例子中直接在浏览器input中输入别的名字,下面的p的内容会直接跟着变。这就是双向数据绑定。

可用修饰符:

.lazy - 默认情况下,v-model同步输入框的值和数据。可以通过这个修饰符,转变为在change事件再同步。

.number - 自动将用户的输入值转化为数值类型

.trim - 自动过滤用户输入的首尾空格

修饰符使用方法:如:

<input v-model.trim="somebody">

v-pre

v-pre主要用来跳过这个元素和它的子元素编译过程。可以用来显示原始的Mustache标签。跳过大量没有指令的节点加快编译。

<div id="app">   <span v-pre>{{message}}</span> //这条语句不进行编译   <span>{{message}}</span> </div>
<div id="app" v-cloak>   <div>     {{message}}   </div> </div> <script type="text/javascript">   new Vue({    el:'#app',    data:{     message:'hello world'    }   }) </script>
<div>   {{message}} </div>

然后才会编译为:

<div>   hello world! </div>

v-cloak指令可以解决上面插值闪烁的问题,如下:其实利用的就是当插值没有被加载出来的是通过 style属性将内容给隐藏了。

  <style>     [v-cloak] {        display: none;      }   </style>      <div id="app">     <!-- 使用 v-cloak 能够解决 插值表达式闪烁的问题 -->     <p v-cloak>++++++++ {{ msg }} ----------</p>   </div>      <script>     var vm = new Vue({       el: '#app',       data: {         msg: 'hello',       }     })   </script>

v-once

v-once关联的实例,只会渲染一次。之后的重新渲染,实例极其所有的子节点将被视为静态内容跳过,这可以用于优化更新性能。

<span v-once>This will never change:{{msg}}</span> //单个元素 <div v-once>//有子元素   <h1>comment</h1>   <p>{{msg}}</p> </div> <my-component v-once:comment="msg"></my-component> //组件 <ul>   <li v-for="i in list">{{i}}</li> </ul>

上面的例子中,msg,list即使产生改变,也不会重新渲染。

v-slot

提供具名插槽或需要接收 prop 的插槽。

可简写为:#

slot 和 scope-slot 是在 vue@2.6.x 之前的语法,而从 vue@2.6.0 开始,官方推荐我们使用 v-slot 来替代前两者。

使用具名插槽来自定义模板内容(vue@2.6.x已经废弃)

<div class="container">   <header>     <slot name="header"></slot>   </header>   <main>     <slot></slot>   </main>   <footer>     <slot name="footer"></slot>   </footer> </div>

在向具名插槽提供内容的时候,我们可以在一个父组件的 元素上使用 slot 特性:

<base-layout>   <template slot="header">     <h1>Here might be a page title</h1>   </template>    <p>A paragraph for the main content.</p>   <p>And another one.</p>    <template slot="footer">     <p>Here's some contact info</p>   </template> </base-layout>

接下来,使用 v-slot 指令改写上面的栗子:

<base-layout>   <template v-slot:header>     <h1>Here might be a page title</h1>   </template>    <p>A paragraph for the main content.</p>   <p>And another one.</p>    <template v-slot:footer>     <p>Here's some contact info</p>   </template> </base-layout>

使用 # 简写代替 v-slot

<base-layout>   <template #header>     <h1>Here might be a page title</h1>   </template>    <p>A paragraph for the main content.</p>   <p>And another one.</p>    <template #footer>     <p>Here's some contact info</p>   </template> </base-layout>

2020年前端vue面试题大汇总(附答案)

vue教程推荐:2020最新的5个vue.js视频教程精选

更多编程相关知识,请访问:编程教学!!

以上就是了解vue.js中的常用指令(总结)的详细内容,更多内容请关注技术你好其它相关文章!

来源链接:
免责声明:
1.资讯内容不构成投资建议,投资者应独立决策并自行承担风险
2.本文版权归属原作所有,仅代表作者本人观点,不代表本站的观点或立场
标签: 指令
上一篇:php获取远程图片并下载保存到本地的方法分析 下一篇:vue watch用法是什么

相关资讯