search
HomeWeb Front-endVue.jsDetailed explanation of the use of vue's template syntax instructions

This article brings you relevant knowledge about vue, which mainly introduces issues related to the use of VUE template instructions. Directives are the template syntax provided by vue for developers. Let’s take a look at the basic structure used to assist developers in rendering pages. I hope it will be helpful to everyone.

Detailed explanation of the use of vue's template syntax instructions

[Related recommendations: javascript video tutorial, web front-end

The concept of instructions

Directives are template syntax provided by vue for developers to assist developers in rendering the basic structure of the page.

The instructions in vue can be divided into the following six categories according to different uses:

  • Content rendering instructions v-text { { }} v-html

  • Attribute binding instruction v-bind => :

  • ##Event binding Instruction v-on =>

    @

  • Two-way binding instruction v-model

  • Conditional rendering instruction v- if v-show

  • List rendering instructions v-for

Content rendering instructions

Content rendering instructions are used to assist development Or render the text content of the DOM element. There are three commonly used content rendering instructions:

    v-text
  • {
  • { }}
  • v-html

Difference:

  • The v-text directive will overwrite the default value in the element

  • {# provided by vue ##{ }} syntax, specifically used to solve the problem that v-text will overwrite the default text content. The technical name for this {

    { }} syntax is interpolation expression. Does not overwrite the default text content in the element.

  • v-text directive and interpolation expression can only render plain text content, and can render strings containing HTML tags into HTML elements of the page
  • Attribute binding directive
v-bind

If you need to dynamically bind the

attribute value

to the attribute of the element, you need to use it v-bind Attribute binding instructionSince the

v-bind instruction

is used very frequently in development, vue officially provides the abbreviation for it Form (abbreviated to English :) Code example:

nbsp;html>



    <meta>
    <meta>
    <meta>
    <title>v-bind 属性绑定指令</title>
    <!-- 1.导入vue 的库文件,在window 全局就有了vue这个构造函数 -->
    <script></script>



    <!-- 3.vue 能控制下面这个div 帮我们把数据填充到div内部 -->
    <div>
        <input>
        
        <hr>
        <img  src="/static/imghwm/default1.png" data-src="photo" class="lazy" alt="Detailed explanation of the use of vue's template syntax instructions" >
        
        <hr>
        <div>tips翻转的结果是:{{ tips.split('').reverse().join('')}}</div>

    </div>

    <!-- 2.创建vue实例对象 -->
    <script>
        // 创建vue的实例对象
        const vw = new Vue({
            el: &#39;#app&#39;,
            // data 对象就是要渲染到页面上的数据
            data: {
                tips: "请输入内容",
                photo: "https://cdn.segmentfault.com/r-ce71be5c/static/logo-b.d865fc97.svg"

            }
        })
    </script>



In the template rendering syntax provided by vue, in addition to supporting simple binding In addition to the data values, it also supports the operation of Javascript expressions, such as

Detailed explanation of the use of vues template syntax instructionsevent binding instructions

v-on

vue provides the

v-on event binding directive

to assist programmers in binding event listeners to DOM elements. The syntax format is as follows:

Note: Native DOM objects have native events such as onclick, oninput, and onkeyup. After replacing them with vue’s event binding form, Detailed explanation of the use of vues template syntax instructions
are: v-on:click, v-on:input, v-on:keyup

The event processing function bound by v-on needs to be declared in the methods node

Detailed explanation of the use of vues template syntax instructionsSince the v-on instruction is used very frequently in development, vue officially provides an abbreviation for it (the abbreviation is

@

in English).

Detailed explanation of the use of vues template syntax instructionsCode example:

nbsp;html>



    <meta>
    <meta>
    <meta>
    <title>v-on 事件绑定指令</title>



    <!-- 3.希望vue 能控制下面这个div 帮我们把数据填充到div内部 -->
    <div>
        <h3 id="count的值为-count">count的值为{{count}}</h3>
        <!-- 可以给函数加(形参) -->
        <!-- <button v-on:click="add(2)">+1</button>
        <button v-on:click="sub">-1</button> -->

        <!-- v-on 简写@ -->
        <button>+1</button>
        <button>-1</button>


    </div>


    <!-- 1.导入vue 的库文件,在window 全局就有了vue这个构造函数 -->
    <script></script>
    <!-- 2.创建vue实例对象 -->
    <script>
        // 创建vue的实例对象
        const vm = new Vue({
            el: &#39;#app&#39;,
            // data 对象就是要渲染到页面上的数据
            data: {
                count: 0
            },
            methods:{
                // add: function(){
                //     console.log("OK");
                // }, 简写
                add(n){
                    // 控制台打印详细信息出来
                    console.log(vm);
                    // this.count += 1;
                    this.count += n;

                },
                sub(){
                    this.count -= 1;
                }
            }
        })
    </script>
    


Event modifier

Call in the event handler function

event.preventDefault()

or event.stopPropagation() is a very common requirement. Therefore, vue provides the concept of event modifiers to assist programmers to more conveniently control the triggering of events. The five commonly used event modifiers are as follows:

Detailed explanation of the use of vues template syntax instructionsThe syntax format is as follows:

Detailed explanation of the use of vues template syntax instructionsCode example:

nbsp;html>



    <meta>
    <meta>
    <meta>
    <title>事件修饰符</title>



    <!-- 3.希望vue 能控制下面这个div 帮我们把数据填充到div内部 -->
    <!-- <div id="app">{{ username }}</div> -->
    <div>
        <a>跳转到百度页面</a>
    </div>

    <!-- 1.导入vue 的库文件,在window 全局就有了vue这个构造函数 -->
    <script></script>
    <!-- 2.创建vue实例对象 -->
    <script>
        // 创建vue的实例对象
        const vw = new Vue({
            el: &#39;#app&#39;,
            // data 对象就是要渲染到页面上的数据
            data: {
               
            },
            methods:{
                show(e){
                    // 在控制台输出,阻止浏览器跳转
                    e.preventDefault()
                    console.log(&#39;点击了a连接&#39;);
                }
            }
        })
    </script>
    



Two-way binding instruction v-model

vue provides

v-model two-way data binding instruction

to assist developers when does not operate the DOM Under the premise, quickly obtain the form data.

nbsp;html>


    <meta>
    <meta>
    <meta>
    <title>双向绑定指令 v-model</title>


    <div>
        <!-- 1. v-model.number 自动将用户输入的值转为数值类型 -->
        <input> +
        <input> =
        <span>{{ num1 + num2}}</span>

        <hr>
        <!-- 2.trim 自动过滤用户输入收尾空白字符 -->
        <input>
        <button>获取用户名</button>
    </div>
    <script></script>
    <script>
        const vw = new Vue({
            el: &#39;#app&#39;,
            data: {
                name: "小脆筒",
                num1: 1,
                num2: 2,
            },
            methods:{
                show(){
                    console.log(`用户名是:"$this.name"`);
                }
            }
        })
    </script>

Modifiers of the v-model directive

In order to facilitate the processing of user input, vue provides 3 modifiers for the v-model directive, which are:

Detailed explanation of the use of vues template syntax instructions
Detailed explanation of the use of vues template syntax instructions

条件渲染指令

条件渲染指令用来辅助开发者按需控制 DOM 的显示与隐藏。条件渲染指令有如下两个,分别是:

  • v-if
  • v-show

Detailed explanation of the use of vues template syntax instructionsv-if 和 v-show 的区别

实现原理不同:

  • v-if 指令会动态地创建或移除 DOM 元素,从而控制元素在页面上的显示与隐藏;

  • v-show 指令会动态为元素添加或移除 style=“display: none;” 样式,从而控制元素的显示与隐藏

性能消耗不同:

  • v-if 有更高的切换开销,而 v-show 有更高的初始渲染开销。因此:

  • 如果需要非常频繁地切换,则使用 v-show 较好

  • 如果在运行时条件很少改变,则使用 v-if 较好

代码案例:

nbsp;html>


    <meta>
    <meta>
    <meta>
    <title>条件渲染指令</title>


    <div>
        <p>v-if 控制</p>
        <p>v-show控制</p>
    </div>
    <script></script>
    <script>
        const vw = new Vue({
            el: &#39;#app&#39;,
            data:{
                // 如果为true,都显示;否则控制台只显示v-show代码被隐藏,v-if直接被移除了
                flag: true
            }
        })
            
        
    </script>

列表渲染指令

vue 提供了 v-for 列表渲染指令,用来辅助开发者基于一个数组来循环渲染一个列表结构。

v-for 指令需要使用 item in items 形式的特殊语法,其中:

  • items 是待循环的数组

  • item 是被循环的每一项

Detailed explanation of the use of vues template syntax instructions

【相关推荐:javascript视频教程web前端

The above is the detailed content of Detailed explanation of the use of vue's template syntax instructions. 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
Vue常见面试题汇总(附答案解析)Vue常见面试题汇总(附答案解析)Apr 08, 2021 pm 07:54 PM

本篇文章给大家分享一些Vue面试题(附答案解析)。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

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

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

vue中props可以传递函数吗vue中props可以传递函数吗Jun 16, 2022 am 10:39 AM

vue中props可以传递函数;vue中可以将字符串、数组、数字和对象作为props传递,props主要用于组件的传值,目的为了接收外面传过来的数据,语法为“export default {methods: {myFunction() {// ...}}};”。

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

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

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

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

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

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

通过9个Vue3 组件库,看看聊前端的流行趋势!通过9个Vue3 组件库,看看聊前端的流行趋势!May 07, 2022 am 11:31 AM

本篇文章给大家分享9个开源的 Vue3 组件库,通过它们聊聊发现的前端的流行趋势,希望对大家有所帮助!

react与vue的虚拟dom有什么区别react与vue的虚拟dom有什么区别Apr 22, 2022 am 11:11 AM

react与vue的虚拟dom没有区别;react和vue的虚拟dom都是用js对象来模拟真实DOM,用虚拟DOM的diff来最小化更新真实DOM,可以减小不必要的性能损耗,按颗粒度分为不同的类型比较同层级dom节点,进行增、删、移的操作。

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
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!