Thuộc tính Vue 'ref'
Ví dụ
Sử dụng thuộc tính ref
để thay đổi văn bản bên trong thẻ <p>
:
<div id="app">
<p ref="pEl">Initial text.</p>
<button v-on:click="changeText">Change text</button>
</div>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script type="module">
const app = Vue.createApp({
methods: {
changeText(){
this.$refs.pEl.innerHTML = "Hello!";
}
}
})
app.mount('#app')
</script>
Hãy tự mình thử »Xem thêm ví dụ dưới đây.
Định nghĩa và cách sử dụng
Thuộc tính ref
được sử dụng để đánh dấu các thành phần trong <template>
, để chúng có thể được truy cập từ đối tượng $refs
bên trong <script>
.
Chúng ta có thể sử dụng thuộc tính ref
và đối tượng $refs
trong Vue để thay thế cho các phương thức trong JavaScript đơn giản như getElementById()
hoặc querySelector()
.
Nếu các phần tử HTML được tạo bằng v-for
có thuộc tính ref
, các phần tử DOM thu được sẽ được thêm vào đối tượng $refs
dưới dạng một mảng, như được minh họa trong ví dụ này:
Thêm ví dụ
ví dụ 1
Văn bản bên trong phần tử <p>
bị thay đổi.
<template>
<h1>Example</h1>
<p>Click the button to put "Hello!" as the text in the green p element.</p>
<button @click="changeVal">Change Text</button><br>
<p ref="pEl" id="pEl">This is the initial text</p>
</template>
<script>
export default {
methods: {
changeVal() {
this.$refs.pEl.innerHTML = "Hello!";
}
}
};
</script>
<style>
#pEl {
background-color: lightgreen;
display: inline-block;
}
</style>
Chạy ví dụ »Ví dụ 2
Văn bản từ thẻ <p>
đầu tiên được sao chép sang thẻ <p>
thứ hai.
<template>
<h1>Example</h1>
<p ref="p1">Click the button to copy this text into the paragraph below.</p>
<button @click="transferText">Transfer text</button>
<p ref="p2">...</p>
</template>
<script>
export default {
methods: {
transferText() {
this.$refs.p2.innerHTML = this.$refs.p1.innerHTML;
}
}
};
</script>
Chạy ví dụ »Ví dụ 3
Phần tử <p>
có cùng nội dung với nội dung được viết trong trường đầu vào.
<template>
<h1>Example</h1>
<p>Start writing inside the input element, and the text will be copied into the last paragraph by the use of the '$refs' object.</p>
<input ref="inputEl" @input="getRefs" placeholder="Write something..">
<p ref="pEl"></p>
</template>
<script>
export default {
methods: {
getRefs() {
this.$refs.pEl.innerHTML = this.$refs.inputEl.value;
}
}
};
</script>
Chạy ví dụ »Ví dụ 4
Nút này hiển thị phần tử danh sách thứ ba được lưu trữ dưới dạng phần tử mảng bên trong đối tượng $refs
.
<template>
<h1>Example</h1>
<p>Click the button to reveal the 3rd list element stored as an array element in the $refs object.</p>
<button @click="getValue">Get the 3rd list element</button><br>
<ul>
<li v-for="x in liTexts" ref="liEl">{{ x }}</li>
</ul>
<pre>{{ thirdEl }}</pre>
</template>
<script>
export default {
data() {
return {
thirdEl: ' ',
liTexts: ['Apple','Banana','Kiwi','Tomato','Lichi']
}
},
methods: {
getValue() {
this.thirdEl = this.$refs.liEl[2].innerHTML;
console.log("this.$refs.liEl = ",this.$refs.liEl);
}
}
};
</script>
<style>
pre {
background-color: lightgreen;
display: inline-block;
}
</style>
Chạy ví dụ »Trang liên quan
Hướng dẫn về Vue: Tham khảo mẫu Vue
Hướng dẫn Vue: Chỉ thị Vue v-for
Tham chiếu Vue: Đối tượng Vue $refs