search
HomeWeb Front-endFront-end Q&AIs there any instruction for vuejs?

Is there any instruction for vuejs?

Sep 27, 2021 pm 06:02 PM
vuejsinstruction

vuejs has instructions. Vuejs instructions start with "v-". They act on HTML elements. The instructions provide some special features. When the instructions are bound to elements, the instructions will add some special behaviors to the bound target elements. You can Think of directives as special HTML features.

Is there any instruction for vuejs?

The operating environment of this tutorial: windows7 system, vue2.9.6 version, DELL G3 computer.

vuejs has instructions.

What are instructions in Vue

Vue.js instructions start with v-, they act on HTML elements, Directives provide some special features. When binding a directive to an element, the directive will add some special behavior to the bound target element. We can think of the directive as a special HTML attribute.

  • VueJS extends HTML with new attributes called directives.

  • ViueJS adds functionality to the application through built-in directives.

  • VueJS allows you to customize directives.

Characteristics of directives

  • All directives are included in the scope of Vue instance management .

  • vueJS directives are extended HTML attributes, prefixed with v-.

  • The v-model directive binds an element value (such as the value of an input field) to the application and stores the value.

vuejs common instructions

Vue.js provides some commonly used built-in instructions. Next we will introduce the following Several built-in commands:

  • v-if command
  • v-show command
  • v-else command
  • v-for command
  • v-bind instruction
  • v-on instruction

v-if instruction

v-if is a conditional rendering instruction , which deletes and inserts elements based on the true or false expression.
Basic syntax:
v-if="expression"
expression is an expression that returns a Boolean value. The expression can be a Boolean attribute. It can also be an expression that returns a Boolean.

<div id="app">
			<div v-if="isMale">男士</div>
			<div v-if="age>=20">age:{{age}}</div>
		</div>
		
var vm = new Vue({
			el: &#39;#app&#39;,
			data: {
				age:25,
				isMale:true,
			}
		})

v-show command

The difference between v-show and v-if.
v-show will render html regardless of whether the condition is true, while v-if will render only if the condition is true.

Let’s look at two screenshots first. The first one is when isMale is true, and the second one is when isMale is true. The picture shows that when isMale is false and the condition is not met, you can see that the html of v-if is not rendered,
and p using v-show only changes its style display: none;


<div id="app">
			<div v-if="isMale">男士v-if</div>
			<div v-show="isMale">男士v-show</div>
		</div>
var vm = new Vue({
			el: &#39;#app&#39;,
			data: {
				isMale:false
			}
		})

v-else directive

The v-else directive is used simultaneously with v-if or v-show, v-if condition If it is not established, the v-else content will be displayed

<div id="app">
			<div v-if="isMale">男士</div>
			<div v-else>女士</div>
		</div>
		var vm = new Vue({
			el: &#39;#app&#39;,
			data: {
				isMale:true
			}
		})

v-for instruction

The v-for instruction renders a list based on an array, which is similar to JavaScript's traversal syntax
v-for="item in list"
list is an array, item is the array element currently traversed
v-for="(item,index) in list"where index is the index of the current loop, The subscript starts from 0

<div id="app">
			<table>
				<tr class="thead">
					<td>序号</td>
					<td>姓名</td>
					<td>年龄</td>
				</tr>
				<tr v-for="(item,index) in list">
					<td v-text="index+1"></td>
					<td v-text="item.name"></td>
					<td v-text="item.age"></td>
				</tr>
			</table>
		</div>
		
var vm = new Vue({
			el: &#39;#app&#39;,
			data: {
				list:[{
					name:&#39;章三&#39;,
					age:18
				},{
					name:&#39;李四&#39;,
					age:23
				}]
			}
		})

v-bind command

v-bind dynamically binds one or more features, You can take a parameter after its name and separate it with a colon. This parameter is usually an attribute of the HTML element. For example, v-bind: class

class can exist at the same time as v-bind:class. , that is to say, if there is a class, adding v-bind:class will not overwrite the original style class, but add a new class name on the original basis

<div id="app">
			<img class="isLogo?&#39;&#39;:&#39;product&#39; lazy"  src="/static/imghwm/default1.png"  data-src="img"  v-bind:  
				v-bind: 
				v-bind:style="{&#39;border&#39;:hasBorder?&#39;2px solid red&#39;:&#39;&#39;}"></img>
		</div>
		
		var vm = new Vue({
			el: &#39;#app&#39;,
			data: {
				img:&#39;https://www.baidu.com/img/bd_logo1.png&#39;,
				isLogo:false,
				hasBorder:true
			}
		})

The above v-bind:src can also be used Abbreviated as: src, modify the above code

<div id="app">
			<img class="isLogo?&#39;&#39;:&#39;product&#39; lazy"  src="/static/imghwm/default1.png"  data-src="img"  :  
				: 
				:style="{&#39;border&#39;:hasBorder?&#39;2px solid red&#39;:&#39;&#39;}"></img>
		</div>

v-on instruction

v-on is used to monitor DOM events. Its usage is similar to v-bind, for example, to button Add click event
<button v-on:click="show"></button>
Similarly, like v-bind, v-on can also use the abbreviation, replaced by the @ symbol, Modify the code:
<button></button>

Let's take a look at an example:

The following is a code that clicks to hide and display p text paragraphs

<div id="app">			<p v-show="isShow">微风轻轻的吹来,带来了一丝丝凉意</p>
			<div>
				<button type="button" v-on:click="show(1)">显示</button>
				<button type="button" v-on:click="show(0)">隐藏</button>
			</div>
		</div>		
		var vm = new Vue({			el: &#39;#app&#39;,			data: {				isShow:true
			},			methods:{				show:function(type){					if(type){						this.isShow = true;
					}else{						this.isShow = false;
					}
				}
			}
		})

Comprehensive example

<div id="app">
			<div class="title">添加新用户</div>
			<div class="form">
				姓名:<input type="text" v-model="person.name"><br/>
				年龄:<input type="text" v-model="person.age"><br/>
				<button class="btn" type="button" @click="add">添加</button>
			</div>
			<table>
				<tr class="thead">
					<td>序号</td>
					<td>姓名</td>
					<td>年龄</td>
					<td>操作</td>
				</tr>
				<tr v-for="(item,index) in list">
					<td v-text="index+1"></td>
					<td v-text="item.name"></td>
					<td v-text="item.age"></td>
					<td>
						<a href="javascript:;" @click="deleteItem(index)">删除</a>
						<a v-if="item.age>=25" href="javascript:;" @click="marry(index)">可以结婚了</a>
					</td>
				</tr>
			</table>
		</div>
		new Vue({
			el: &#39;#app&#39;,
			data: {
				person:{
					name:&#39;&#39;,
					age:&#39;&#39;,
				},
				list:[{
					name:&#39;章三&#39;,
					age:18
				},{
					name:&#39;李四&#39;,
					age:23
				}]
			},
			methods:{
				add:function(){
					this.list.push(this.person);
					this.person = {name:&#39;&#39;,age:&#39;&#39;};
				},
				deleteItem:function(index){
					// 删除一个数组元素
					this.list.splice(index,1);
				},
				marry:function(){
					alert("不好意思,你没有女朋友结不了婚");
				}
			},
			created:function(){
			}
		})

Related recommendations:《 vue.js tutorial

The above is the detailed content of Is there any instruction for vuejs?. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
HTML and React's Integration: A Practical GuideHTML and React's Integration: A Practical GuideApr 21, 2025 am 12:16 AM

HTML and React can be seamlessly integrated through JSX to build an efficient user interface. 1) Embed HTML elements using JSX, 2) Optimize rendering performance using virtual DOM, 3) Manage and render HTML structures through componentization. This integration method is not only intuitive, but also improves application performance.

React and HTML: Rendering Data and Handling EventsReact and HTML: Rendering Data and Handling EventsApr 20, 2025 am 12:21 AM

React efficiently renders data through state and props, and handles user events through the synthesis event system. 1) Use useState to manage state, such as the counter example. 2) Event processing is implemented by adding functions in JSX, such as button clicks. 3) The key attribute is required to render the list, such as the TodoList component. 4) For form processing, useState and e.preventDefault(), such as Form components.

The Backend Connection: How React Interacts with ServersThe Backend Connection: How React Interacts with ServersApr 20, 2025 am 12:19 AM

React interacts with the server through HTTP requests to obtain, send, update and delete data. 1) User operation triggers events, 2) Initiate HTTP requests, 3) Process server responses, 4) Update component status and re-render.

React: Focusing on the User Interface (Frontend)React: Focusing on the User Interface (Frontend)Apr 20, 2025 am 12:18 AM

React is a JavaScript library for building user interfaces that improves efficiency through component development and virtual DOM. 1. Components and JSX: Use JSX syntax to define components to enhance code intuitiveness and quality. 2. Virtual DOM and Rendering: Optimize rendering performance through virtual DOM and diff algorithms. 3. State management and Hooks: Hooks such as useState and useEffect simplify state management and side effects handling. 4. Example of usage: From basic forms to advanced global state management, use the ContextAPI. 5. Common errors and debugging: Avoid improper state management and component update problems, and use ReactDevTools to debug. 6. Performance optimization and optimality

React's Role: Frontend or Backend? Clarifying the DistinctionReact's Role: Frontend or Backend? Clarifying the DistinctionApr 20, 2025 am 12:15 AM

Reactisafrontendlibrary,focusedonbuildinguserinterfaces.ItmanagesUIstateandupdatesefficientlyusingavirtualDOM,andinteractswithbackendservicesviaAPIsfordatahandling,butdoesnotprocessorstoredataitself.

React in the HTML: Building Interactive User InterfacesReact in the HTML: Building Interactive User InterfacesApr 20, 2025 am 12:05 AM

React can be embedded in HTML to enhance or completely rewrite traditional HTML pages. 1) The basic steps to using React include adding a root div in HTML and rendering the React component via ReactDOM.render(). 2) More advanced applications include using useState to manage state and implement complex UI interactions such as counters and to-do lists. 3) Optimization and best practices include code segmentation, lazy loading and using React.memo and useMemo to improve performance. Through these methods, developers can leverage the power of React to build dynamic and responsive user interfaces.

React: The Foundation for Modern Frontend DevelopmentReact: The Foundation for Modern Frontend DevelopmentApr 19, 2025 am 12:23 AM

React is a JavaScript library for building modern front-end applications. 1. It uses componentized and virtual DOM to optimize performance. 2. Components use JSX to define, state and attributes to manage data. 3. Hooks simplify life cycle management. 4. Use ContextAPI to manage global status. 5. Common errors require debugging status updates and life cycles. 6. Optimization techniques include Memoization, code splitting and virtual scrolling.

The Future of React: Trends and Innovations in Web DevelopmentThe Future of React: Trends and Innovations in Web DevelopmentApr 19, 2025 am 12:22 AM

React's future will focus on the ultimate in component development, performance optimization and deep integration with other technology stacks. 1) React will further simplify the creation and management of components and promote the ultimate in component development. 2) Performance optimization will become the focus, especially in large applications. 3) React will be deeply integrated with technologies such as GraphQL and TypeScript to improve the development experience.

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

Video Face Swap

Video Face Swap

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

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools