Home > Article > Web Front-end > How to get DOM elements and set attributes with vue
I thought of 2 methods here:
Method 1:
Add id directly to the corresponding element, and then
document.getElementById("id");
get it, and then set the corresponding attribute or Style
Method 2:
Use ref, add ref="name" to the corresponding element and then use this.$refs.name to get the element
Note: Before obtaining the corresponding element, you must mount it in the mount hook, otherwise the obtained value will be empty,
If you add an ID to a sub-component and modify the custom attribute, the modified sub-component will be loaded directly On the corresponding outer p, the value of the original custom attribute of the subcomponent will not be changed
If you add ref to the subcomponent, and then change the value of the corresponding custom attribute after obtaining the DOM element, vue will report an error:
Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "title"
The above is the detailed content of How to get DOM elements and set attributes with vue. For more information, please follow other related articles on the PHP Chinese website!