Discussion on components and templates in Vue.js
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('
Vue’s built-in instructions
1. v-bindv-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:'123' }; //声明式渲染 var vm = new Vue({ el:'#demo', data:obj }); </script> </body> </html>
v-bind can be abbreviated as ":",
The above example can be abbreviated as
The implementation effect is as follows:
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>
The effect is as follows:
##3.v- html
v-html, the parameter type is string,
is used to update innerHTML, acceptedstring
will not be compiled Wait for the operation, 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>
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 htmlInterpolation 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 htmlAttributes: use v-bind Binding can respond to changesUse 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>
There can only be one root node
<!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>
Writing in script tags is still relatively limited.
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>
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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

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

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

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

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

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 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
