Home Web Front-end Front-end Q&A How to get custom attributes in vue

How to get custom attributes in vue

May 25, 2023 am 09:32 AM

In Vue, we can bind custom attributes to elements through the v-bind directive, and then obtain these custom attributes through JavaScript. Let's learn step by step how to get custom attributes.

  1. Bind custom attributes to elements

The v-bind directive allows us to dynamically bind attributes to elements in the form: v-bind: attribute name ="expression", or abbreviated as: attribute name="expression".

We can bind a custom attribute data-id to the element like this:

<template>
  <div>
    <p v-bind:data-id="userId">User ID</p>
  </div>
</template>
Copy after login

Among them, userId is a variable defined in the data of the component.

  1. Get custom attributes

We can get this custom attribute through JavaScript. In Vue, we can get the custom attributes of the element during the mounted() life cycle. The mounted() life cycle is a hook function triggered after the Vue instance is mounted on the DOM. At this time, the DOM can be manipulated.

Suppose we have bound the data-id attribute to the p element in the previous component, then we can get the attribute like this:

<template>
  <div>
    <p v-bind:data-id="userId" ref="user">User ID</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      userId: '123456'
    }
  },
  mounted() {
    const userEle = this.$refs.user;  //获取p元素
    const userId = userEle.getAttribute('data-id');  //获取自定义属性
    console.log(userId);  //打印出123456
  }
}
</script>
Copy after login

In the above example, we bind the p element The custom attribute data-id is bound, and a reference named "User" is given to the p element through the ref attribute. In the mounted() function, we obtain the p element through this.$refs.user, and call the getAttribute('data-id') method on it to obtain the custom attribute. Finally, we print out the obtained value, and the result is 123456.

  1. Using custom attributes

After obtaining the custom attributes, we can perform some corresponding operations. For example, when we click on the p element, the value of the element's custom attribute pops up:

<template>
  <div>
    <p v-bind:data-id="userId" ref="user" @click="showId">User ID</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      userId: '123456'
    }
  },
  methods: {
    showId() {
      const userEle = this.$refs.user;  //获取p元素
      const userId = userEle.getAttribute('data-id');  //获取自定义属性
      alert(userId);  //弹出该元素自定义属性的值
    }
  }
}
</script>
Copy after login

The above is how to get the custom attribute in Vue. Bind custom properties through the v-bind directive, and then obtain these custom properties through JavaScript. Finally, you can use these properties in the need to extend the functionality of Vue.

The above is the detailed content of How to get custom attributes in vue. 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

What is useEffect? How do you use it to perform side effects? What is useEffect? How do you use it to perform side effects? Mar 19, 2025 pm 03:58 PM

What is useEffect? How do you use it to perform side effects?

Explain the concept of lazy loading. Explain the concept of lazy loading. Mar 13, 2025 pm 07:47 PM

Explain the concept of lazy loading.

How does currying work in JavaScript, and what are its benefits? How does currying work in JavaScript, and what are its benefits? Mar 18, 2025 pm 01:45 PM

How does currying work in JavaScript, and what are its benefits?

What are higher-order functions in JavaScript, and how can they be used to write more concise and reusable code? What are higher-order functions in JavaScript, and how can they be used to write more concise and reusable code? Mar 18, 2025 pm 01:44 PM

What are higher-order functions in JavaScript, and how can they be used to write more concise and reusable code?

What is useContext? How do you use it to share state between components? What is useContext? How do you use it to share state between components? Mar 19, 2025 pm 03:59 PM

What is useContext? How do you use it to share state between components?

How does the React reconciliation algorithm work? How does the React reconciliation algorithm work? Mar 18, 2025 pm 01:58 PM

How does the React reconciliation algorithm work?

How do you prevent default behavior in event handlers? How do you prevent default behavior in event handlers? Mar 19, 2025 pm 04:10 PM

How do you prevent default behavior in event handlers?

What are the advantages and disadvantages of controlled and uncontrolled components? What are the advantages and disadvantages of controlled and uncontrolled components? Mar 19, 2025 pm 04:16 PM

What are the advantages and disadvantages of controlled and uncontrolled components?

See all articles