Home Web Front-end JS Tutorial Discussion on components and templates in Vue.js

Discussion on components and templates in Vue.js

Oct 28, 2017 am 09:29 AM
javascript vue.js Discuss

Abstract:

Directive is an important feature in Vue.js. It mainly provides a mechanism to map data changes to DOM behavior. What changes in data are mapped to DOM behaviors? Vue.js is driven by data, so we will not directly modify the DOM structure, and there will be no similar $('ul').append('

  • one
  • ') such an operation, when the data changes, the instruction will modify the DOM with a set operation, so that you can only focus on the data changes without having to manage DOM changes and Status,

    Vue’s built-in instructions

    1. v-bind

    v-bind It is mainly used to bind DOM element attributes (attributes).

    That is, the actual value of the element attribute is provided by the data attribute in the vm instance.

    For example:

    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>Vue的指令</title>
      <script src="../vue.js"></script>
    </head>
    <body>
    <!-- HTML模版 -->
    <p id="demo">
      <span v-bind:cutomId="id">{{message}}</span>
    </p>
    <script>
      //数据
      let obj ={
        message:"Hello World",
        id:&#39;123&#39;
      };
     //声明式渲染
      var vm = new Vue({
        el:&#39;#demo&#39;,
        data:obj  });
    </script>
    </body>
    </html>
    Copy after login

    v-bind can be abbreviated as ":",

    The above example can be abbreviated as

    The implementation effect is as follows:

    2. v-on

    Bind event listener , abbreviated as @.

    We also used it yesterday, let’s abbreviate it to see the effect

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>Vue的指令</title>
      <script src="../vue.js"></script>
    </head>
    <body>
      <!-- HTML模版 -->
      <p id="demo">
        <span @click="clickHandle">{{message}}</span>
      </p>
      <script>
        //数据
        let obj = {
          message:"hello Vue"
        };
        //声明式渲染
        var vm = new Vue({
          el:"#demo",
          data:obj,
          methods:{
            clickHandle(){
                alert("click")
                }
          }
        });
      </script>
    </body>
    </html>
    Copy after login

    The effect is as follows:

    ##3.v- html

    v-html, the parameter type is string,

    is used to update innerHTML,

    accepted

    string

    will not be compiled Wait for the operation,

    Process it as normal HTML

    The code is as follows

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>Vue的指令</title>
      <script src="../vue.js"></script>
    </head>
    <body>
    <!-- HTML模版 -->
    <p id="demo" v-html="HTML"></p>
    <script>
      //数据
      let obj = {
        HTML:"<p>Hello World</p>"
      };
      var vm = new Vue({
        el:"#demo",
        data:obj  })
    </script>
    </body>
    </html>
    Copy after login

    The effect is as follows

    For more built-in instructions, please check the official website: Vue.js instructions

    template

    html template

    DOM-based templates, templates are all available Parse valid html

    Interpolation

    Text: Use "Mustache" syntax (double curly brackets) {{value}}

    Function: Replace the attribute value on the instance,

    When the value changes, the interpolated content will automatically update

    Native html: double curly braces output text and will not parse html

    Attributes: use v-bind Binding can respond to changes

    Use JavaScript

    Expression: You can write simple expressionsString template

    template string

                                                                                                    --   Attribute of template option object

                                                                                                                ’ s ’ ’ s ’ s ’ s through through ’s ’ using ’s ’ using ’s ’ through through ’s through through ’s' ’ ‐ to ‐‐‐‐‐‐mn to Content hanging from the element will be ignored.

    The code is as follows

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>template模板</title>
      <script src="../vue.js"></script>
    </head>
    <body>
      <!--HTML模板-->
      <p id="demo"></p>
      <script>
        //数据
        let obj = {
          html:"<p>String</p>",
          abc:1
        };
        var str = "<p>Hello</p>";
        var vm = new Vue({
          el:"#demo",
          data:obj,
          template:str    })
      </script>
    </body>
    </html>
    Copy after login
    Have you noticed any surprising changes

    There can only be one root node

    Write the html structure in a pair of script tags, set type="X-template"

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>template模板</title>
      <script src="../vue.js"></script>
    </head>
    <body>
      <!--HTML模板-->
      <p id="demo">
        <span>vue</span>
      </p>
      <script type="x-template" id="temp">
        <p>
          Hello,{{abc}},
          <span>sunday</span>
        </p>
      </script>
      <script>
        //数据
        let obj = {
          html:"<p>String</p>",
          abc:1
        };
        var vm = new Vue({
          el:"#demo",
          data:obj,
          template:"#temp"
        });
      </script>
    </body>
    </html>
    Copy after login

    The effect is as follows:

    Writing in script tags is still relatively limited.

    If other files also have this structure,

    this cannot be reused.

    Template render function

    render function

    Attributes of render option object

    createElement(tag name, {data object}, [child element]);

    The child elements are text or arrays

    Let’s use a piece of code to demonstrate

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>render函数</title>
      <script src="../vue.js"></script>
      <style type="text/css">
        .bg{
          background: #ee0000;
        }
      </style>
    </head>
    <body>
      <p id="demo"></p>
      <script>
        //数据
        let obj = {
        };
        var vm = new Vue({
          el:"#demo",
          data:obj,
          render(createElement){
            return createElement(
              //元素名
              "ul",
              //数据对象
              {
                class:{
                  bg:true
                }
               },
              //子元素
              [
                createElement("li",1),
                createElement("li",2),
                createElement("li",3)
              ]
            );
          }
        })
      </script>
    </body>
    </html>
    Copy after login
    The effect is as follows

    Summarize

    The above is the detailed content of Discussion on components and templates in Vue.js. For more information, please follow other related articles on the PHP Chinese website!

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    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)
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. How to Fix Audio if You Can't Hear Anyone
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    WWE 2K25: How To Unlock Everything In MyRise
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

    JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

    How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

    Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

    Deep dive: What is the Django framework? Deep dive: What is the Django framework? Jan 19, 2024 am 09:23 AM

    The Django framework is a Python framework for web applications that provides a simple and powerful way to create web applications. In fact, Django has become one of the most popular Python web development frameworks and has become the first choice for many companies, including Instagram and Pinterest. This article will delve into what the Django framework is, including basic concepts and important components, as well as specific code examples. Django basic conceptsDjan

    How to implement an online electronic signature system using WebSocket and JavaScript How to implement an online electronic signature system using WebSocket and JavaScript Dec 18, 2023 pm 03:09 PM

    Overview of how to use WebSocket and JavaScript to implement an online electronic signature system: With the advent of the digital age, electronic signatures are widely used in various industries to replace traditional paper signatures. As a full-duplex communication protocol, WebSocket can perform real-time two-way data transmission with the server. Combined with JavaScript, an online electronic signature system can be implemented. This article will introduce how to use WebSocket and JavaScript to develop a simple online

    A deep dive into the Head request method in Laravel A deep dive into the Head request method in Laravel Mar 06, 2024 pm 03:36 PM

    As a popular PHP framework, Laravel provides many convenient request methods to handle different types of HTTP requests. Among them, the Head request method is a special and often overlooked method. In this article, we will delve into the role, usage and sample code of the Head request method in Laravel. What is the Head request method? The Head request method is a request method defined in the HTTP protocol. When sending a Head request, the server will only return the request header information and not the

    An in-depth discussion of the compatibility of Go language with C language An in-depth discussion of the compatibility of Go language with C language Mar 07, 2024 pm 02:45 PM

    Go language is a programming language developed by Google, which has the characteristics of efficiency, simplicity, and strong concurrency. It has great advantages in syntax structure, package management, advanced features, etc., so it is favored by programmers. However, in actual development, many projects will involve interacting with the traditional programming language C, so the compatibility of Go language and C language is particularly important. First, let’s talk about the compatibility of Go language and C language. In the Go language, you can interact with the C language through CGo. CGo

    Deep dive: Single-threaded features in Go language Deep dive: Single-threaded features in Go language Mar 15, 2024 pm 02:09 PM

    As a modern programming language, Go language has been loved and favored by more and more developers in recent years due to its simplicity and efficiency. One of the unique features is its single-threaded nature. In traditional multi-threaded programming languages, developers usually need to manually manage synchronization and mutual exclusion between threads. In Go language, with its unique coroutine (Goroutine) and communication mechanism (channel), it can be convenient and efficient. implement concurrent programming. 1. Goroutine and single thread: Go language

    How to implement a real-time online voting system using JavaScript and WebSocket How to implement a real-time online voting system using JavaScript and WebSocket Dec 18, 2023 pm 04:27 PM

    How to use JavaScript and WebSocket to implement a real-time online voting system Introduction: With the rapid development of the Internet, real-time online voting systems have become a very common form in various activities and elections. Using JavaScript and WebSocket technology to implement a real-time online voting system has the advantages of flexibility and ease of use. This article will introduce in detail how to use JavaScript and WebSocket to implement a simple real-time online voting system and provide the corresponding code

    See all articles