search
HomeWeb Front-endVue.jsUnderstand common instructions in vue.js (summary)

Understand common instructions in vue.js (summary)

v-text

v-text is mainly used to update textContent, which can be equivalent to the text attribute of JS.

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

v-html

The double brace method will interpret the data as plain text, not HTML. In order to output real HTML, you can use the v-html command. It is equivalent to JS's innerHtml property.

Note: Content is inserted as normal HTML - will not be compiled as a Vue template.

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

v-show

Equivalent to css's dispaly attribute switching between "none" and "block" settings.

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

v-if

v-if can implement conditional rendering. Vue will render elements based on the true and false conditions of the expression value. .

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

The above code, if isShow is false, the div is rendered, otherwise it is not rendered.

Note:

v-if needs to be distinguished from v-show. The elements of v-show will always be rendered and saved in the dom. It just simply switches the dispaly attribute of css.

v-if has higher switching overhead.

v-show has higher initial rendering overhead.

So, if you want to switch very frequently, it is better to use v-show; if the conditions are unlikely to change during runtime, it is better to use v-if.

v-else

v-else is used with v-if, it must follow v-if or v-else -if after, otherwise it has no effect.
Similar to JS’s if .. else.

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

v-else-if

v-else-if acts as the else-if block of v-if and can be used in a chain repeatedly. Similar to JS's if .. else if .. else

<div v-if="type===&#39;A&#39;">
  A
</div>
<div v-if="type===&#39;B&#39;">
  B
</div>
<div v-if="type===&#39;C&#39;">
  C
</div>
<div v-else>
  Not A,B,C
</div>

v-for

Use the v-for instruction to render by traversing the array .

<ul>
	<li v-for="item in items">{{item.name}}</li>
</ul>

<script>
new Vue({
  el: &#39;#app&#39;,
  data: {
    items: [
      { name: &#39;Runoob&#39; },
      { name: &#39;Google&#39; },
      { name: &#39;Taobao&#39; }
    ]
  }
})
</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

Bind event listener. The event type is specified by parameters. The expression can be the name of a method or an inline statement, and can be omitted if there are no modifiers.
v-on can also be abbreviated as "@", such as:

v-on="show" can be abbreviated as: @show

<!-- 方法处理器 -->
<button v-on:click="doThis"></button>

<!-- 动态事件 (2.6.0+) -->
<button v-on:[event]="doThis"></button>

<!-- 内联语句 -->
<button v-on:click="doThat(&#39;hello&#39;, $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>

You can also use modifiers, as follows :

.stop - Call event.stopPropagation().

.prevent - Calls event.preventDefault().

.capture - Use capture mode when adding event listeners.

.self - The callback is only triggered when the event is triggered from the element itself to which the listener is bound.

.{keyCode | keyAlias} - The callback is only fired when the event is triggered from a specific key.

.native - Listen to native events on the root element of the component.

.once - The callback is triggered only once.

.left - only fires when the left mouse button is clicked.

.right - only triggered when the right mouse button is clicked.

.middle - only triggered when the middle mouse button is clicked.

.passive - add a listener in { passive: true } mode

v-bind

Dynamically bind one or more attributes, or a component prop to an expression. Often used to dynamically bind classes and styles. And href etc.

can be abbreviated as: " : ", such as:

v-bind:class=" isActive : 'active' :' ' ", can be abbreviated as: :class=" isActive : ' active' :' ' "

<div v-bind:class=" isActive : &#39;active&#39; :&#39; &#39; "></div>
<script>
  var app = new Vue({
    el: &#39;#app&#39;,
    data: {
      isActive : true, 
    }
  })
</script>

//渲染结果为: <div class="active"></div>

Bind multiple classes, the details are as follows:

<div v-bind:class="[ isActive : &#39;active&#39; :&#39; &#39; , isError: &#39;error&#39; :&#39; &#39; ]">
</div>
<script>
  var app = new Vue({
    el: &#39;#app&#39;,
    data: {
      isActive : true, 
      isError:  true,
    }
  })
</script>

//渲染结果为: <div class="active error"></div>

Other examples, see the code below for details:

<!-- 绑定一个属性 -->
<img  src="/static/imghwm/default1.png"  data-src="imageSrc"  class="lazy"  v-bind: alt="Understand common instructions in vue.js (summary)" >

<!-- 动态特性名 (2.6.0+) -->
<button v-bind:[key]="value"></button>

<!-- 缩写 -->
<img  src="/static/imghwm/default1.png"  data-src="imageSrc"  class="lazy"  : alt="Understand common instructions in vue.js (summary)" >

<!-- 动态特性名缩写 (2.6.0+) -->
<button :[key]="value"></button>

<!-- 内联字符串拼接 -->
<img  src="/static/imghwm/default1.png"  data-src="&#39;/path/to/images/&#39; + fileName"  class="lazy"  : alt="Understand common instructions in vue.js (summary)" >

<!-- class 绑定 -->
<div :class="{ red: isRed }"></div>
<div :class="[classA, classB]"></div>
<div :class="[classA, { classB: isB, classC: isC }]">

<!-- style 绑定 -->
<div :style="{ fontSize: size + &#39;px&#39; }"></div>
<div :style="[styleObjectA, styleObjectB]"></div>

<!-- 绑定一个有属性的对象 -->
<div v-bind="{ id: someProp, &#39;other-attr&#39;: 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

#Create two-way binding on a form control or component.
v-model will ignore the initial values ​​of the value, checked, and selected attributes of all form elements. Because it selects Vue instance data as the specific value.

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

In this example, enter another name directly into the browser input, and the content of the p below will change directly accordingly. This is two-way data binding.

Available modifiers:

.lazy - By default, v-model synchronizes the value and data of the input box. You can use this modifier to switch to resynchronization in the change event.

.number - Automatically convert the user's input value into a numerical type

.trim - Automatically filter the leading and trailing spaces entered by the user

How to use modifiers: For example:

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

v-pre

v-pre is mainly used to skip the compilation process of this element and its sub-elements. Can be used to display the original Mustache tag. Skip a large number of nodes without instructions to speed up compilation.

<div id="app">
  <span v-pre>{{message}}</span> //这条语句不进行编译
  <span>{{message}}</span>
</div>

This directive is used to remain on the element until the end of the associated instance for compilation.

<div id="app" v-cloak>
  <div>
    {{message}}
  </div>
</div>
<script type="text/javascript">
  new Vue({
   el:&#39;#app&#39;,
   data:{
    message:&#39;hello world&#39;
   }
  })
</script>

Explanation:
will flash when the page is loaded. It will first display:

<div>
  {{message}}
</div>

and then compile to:

<div>
  hello world!
</div>

The v-cloak command can To solve the problem of interpolation flickering above, as follows: In fact, what is used is to hide the content through the style attribute when the interpolation is not loaded.

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

v-once

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

<span v-once>This will never change:{{msg}}</span> //单个元素
<div v-once>//有子元素
  <h1 id="comment">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 id="Here-nbsp-might-nbsp-be-nbsp-a-nbsp-page-nbsp-title">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&#39;s some contact info</p>
  </template>
</base-layout>

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

<base-layout>
  <template v-slot:header>
    <h1 id="Here-nbsp-might-nbsp-be-nbsp-a-nbsp-page-nbsp-title">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&#39;s some contact info</p>
  </template>
</base-layout>

使用 # 简写代替 v-slot

<base-layout>
  <template #header>
    <h1 id="Here-nbsp-might-nbsp-be-nbsp-a-nbsp-page-nbsp-title">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&#39;s some contact info</p>
  </template>
</base-layout>

相关推荐:

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

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

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

The above is the detailed content of Understand common instructions in vue.js (summary). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete
总结分享几个 VueUse 最佳组合,快来收藏使用吧!总结分享几个 VueUse 最佳组合,快来收藏使用吧!Jul 20, 2022 pm 08:40 PM

VueUse 是 Anthony Fu 的一个开源项目,它为 Vue 开发人员提供了大量适用于 Vue 2 和 Vue 3 的基本 Composition API 实用程序函数。本篇文章就来给大家分享几个我常用的几个 VueUse 最佳组合,希望对大家有所帮助!

5 款适合国内使用的 Vue 移动端 UI 组件库5 款适合国内使用的 Vue 移动端 UI 组件库May 05, 2022 pm 09:11 PM

本篇文章给大家分享5 款适合国内使用的 Vue 移动端 UI 组件库,希望对大家有所帮助!

聊聊Vue3+qrcodejs如何生成二维码并添加文字描述聊聊Vue3+qrcodejs如何生成二维码并添加文字描述Aug 02, 2022 pm 09:19 PM

Vue3如何更好地使用qrcodejs生成二维码并添加文字描述?下面本篇文章给大家介绍一下Vue3+qrcodejs生成二维码并添加文字描述,希望对大家有所帮助。

手把手带你利用vue3.x绘制流程图手把手带你利用vue3.x绘制流程图Jun 08, 2022 am 11:57 AM

利用vue3.x怎么绘制流程图?下面本篇文章给大家分享基于 vue3.x 的流程图绘制方法,希望对大家有所帮助!

Github 上 8 个不可错过的 Vue 项目,快来收藏!!Github 上 8 个不可错过的 Vue 项目,快来收藏!!Jun 17, 2022 am 10:37 AM

本篇文章给大家整理分享8个GitHub上很棒的的 Vue 项目,都是非常棒的项目,希望当中有您想要收藏的那一个。

如何使用VueRouter4.x?快速上手指南如何使用VueRouter4.x?快速上手指南Jul 13, 2022 pm 08:11 PM

如何使用VueRouter4.x?下面本篇文章就来给大家分享快速上手教程,介绍一下10分钟快速上手VueRouter4.x的方法,希望对大家有所帮助!

聊聊vue指令中的修饰符,常用事件修饰符总结聊聊vue指令中的修饰符,常用事件修饰符总结May 09, 2022 am 11:07 AM

本篇文章带大家聊聊vue指令中的修饰符,对比一下vue中的指令修饰符和dom事件中的event对象,介绍一下常用的事件修饰符,希望对大家有所帮助!

如何覆盖组件库样式?React和Vue项目的解决方法浅析如何覆盖组件库样式?React和Vue项目的解决方法浅析May 16, 2022 am 11:15 AM

如何覆盖组件库样式?下面本篇文章给大家介绍一下React和Vue项目中优雅地覆盖组件库样式的方法,希望对大家有所帮助!

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft