Home> Web Front-end> Vue.js> body text

An article explaining the vue directive and its filters in detail (with code examples)

藏色散人
Release: 2023-01-22 07:30:01
forward
1940 people have browsed it

This article brings you relevant knowledge about front-end vue. Let’s talk about what content rendering instructions and attribute binding instructions are. Friends who are interested, let’s take a look together. I hope it will be useful to friends who need it. Helps!

An article explaining the vue directive and its filters in detail (with code examples)

vue directives and filters

Content rendering directive

The content rendering directive is used to assist developers in rendering the text content of DOM elements . There are three commonly used content rendering instructions.

v-text

Example

性别

Copy after login
//创建vue 的实例对象 const vm = new Vue({ //el 属性是固定写法,表示当前 vm 实例要控制的区域,接收的是一个选择器 el: '#app', // data 对象就是要渲染到页面上的数据 data: { username: 'zs', gender: '男' } });
Copy after login

Interpolation expression{{}}Double braces

Application in actual development More, it will not overwrite the original rendering
Example

姓名:{{username}}

Copy after login
const vm = new Vue({ //el 属性是固定写法,表示当前 vm 实例要控制的区域,接收的是一个选择器 el: '#app', // data 对象就是要渲染到页面上的数据 data: { username: 'zs', gender: '男', } });
Copy after login

v-html

You can render the tagged string into real html content
Example

Copy after login
const vm = new Vue({ //el 属性是固定写法,表示当前 vm 实例要控制的区域,接收的是一个选择器 el: '#app', // data 对象就是要渲染到页面上的数据 data: { info: '

欢迎学习 vuejs

' } });
Copy after login

Attribute binding instructions

Note: Interpolation expressions can only be used in element content nodes, not in element attribute nodes

Dynamic binding Define the attribute value v-bind

Add the attribute instruction before the attributev-bind:dynamically bind the value to the element, vue stipulates thatv-bindcan be abbreviated as:, Example

 
Copy after login

Using javascript expressions

In the template rendering syntax provided by vue, in addition to supportingbinding simple data values, also supports the operation of javascript expressions, such as

{{ number + 1 }}; {{ ok ? 'YES' : 'NO'}}; {{ message.split('').reverse().join('')}}; 
Copy after login

Note that during the abbreviation v-bind attribute binding, if the binding content needs to be dynamically spliced, the string should be wrapped in single quotes, such as

!!!!!
Copy after login

Event binding instruction

v-on Binding event

v-on Binding event instruction assists programmers in binding listening events for DOM elements. The format is as follows

count的值是: {{count}}

Copy after login
const vm = new Vue({ //el 属性是固定写法,表示当前 vm 实例要控制的区域,接收的是一个选择器 el: '#app', // data 对象就是要渲染到页面上的数据 data:{ count: 0, }, // 定义事件的处理函数 methods:{ add: function () { // console.log(vm); // vm.count += 1; // this === vm this.count += 1; } // 也可简写成 add () { // console.log(vm); this.count += 1; } } });
Copy after login

v-on:can also be abbreviated as @

Copy after login

Note: The native DOM object has native events such as onclick, oninput, onkeydown, etc., replace them with vue After the event binding form, they are: v-on:click, v-on:input, v-on:keydown

Event object

vue provides built-in fixed variables$event, which is the event object of the native DOM e

  
Copy after login
const vm = new Vue({ //el 属性是固定写法,表示当前 vm 实例要控制的区域,接收的是一个选择器 el: '#app', // data 对象就是要渲染到页面上的数据 data:{ count: 0, }, // 定义事件的处理函数 methods:{ add (n, e) { this.count += 1; // 判断 this.count 的值是否为偶数 if (this.count%2 === 0) { //偶数 e.target.style.backgroundColor = 'blue'; console.log(e); } else { // 奇数 e.target.style.backgroundColor = ''; } } } });
Copy after login

Event modifier

Callevent.preventDefault()or ## in the event processing function #event.stopPROpagation()is a very common requirement. Therefore, vue provides the concept of event modification to assist programmers to more conveniently control the triggering of events. The five commonly used modifiers are as follows:

Event modifier Description to Capture mode triggers the current event processing function ##.once .self event.target Example 1:
.prevent Prevent default behavior(for example: prevent a link jump, prevent form submission, etc.)
.stop Stop event bubbling
.capture
The bound event only triggers once
The event handler function is only triggered whenis the current element itself
跳转到百度首页
Copy after login
const vm = new Vue({ //el 属性是固定写法,表示当前 vm 实例要控制的区域,接收的是一个选择器 el: '#app', // data 对象就是要渲染到页面上的数据 data:{}, // 定义事件的处理函数 methods:{ show () { // e.preventDefault(); console.log("点击了 a 链接"); } } });
Copy after login

Recommended learning: "

vue.js video tutorial

"

The above is the detailed content of An article explaining the vue directive and its filters in detail (with code examples). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
vue
source:cnblogs.com
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