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

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

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

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.

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.

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.

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