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

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    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

    Vue.js vs. React: Project-Specific Considerations Vue.js vs. React: Project-Specific Considerations Apr 09, 2025 am 12:01 AM

    Vue.js is suitable for small and medium-sized projects and fast iterations, while React is suitable for large and complex applications. 1) Vue.js is easy to use and is suitable for situations where the team is insufficient or the project scale is small. 2) React has a richer ecosystem and is suitable for projects with high performance and complex functional needs.

    Is vue.js hard to learn? Is vue.js hard to learn? Apr 04, 2025 am 12:02 AM

    Vue.js is not difficult to learn, especially for developers with a JavaScript foundation. 1) Its progressive design and responsive system simplify the development process. 2) Component-based development makes code management more efficient. 3) The usage examples show basic and advanced usage. 4) Common errors can be debugged through VueDevtools. 5) Performance optimization and best practices, such as using v-if/v-show and key attributes, can improve application efficiency.

    Is Vue used for frontend or backend? Is Vue used for frontend or backend? Apr 03, 2025 am 12:07 AM

    Vue.js is mainly used for front-end development. 1) It is a lightweight and flexible JavaScript framework focused on building user interfaces and single-page applications. 2) The core of Vue.js is its responsive data system, and the view is automatically updated when the data changes. 3) It supports component development, and the UI can be split into independent and reusable components.

    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 common special characters in Linux A deep dive into common special characters in Linux Mar 14, 2024 pm 02:54 PM

    As a commonly used open source operating system, Linux operating system has strong customizability and flexibility. When using Linux systems, we often encounter the processing of various special characters. These special characters have special meanings in the command line and can implement many advanced functions. This article will delve into the common special characters in Linux and introduce their usage in detail with specific code examples. Wildcards: Wildcards are special characters used to match file names. Common wildcards include *,?, [], etc. Here are several

    See all articles