Vue ref 属性

实例

使用 ref 属性更改 <p> 标记内的文本:

  1. <div id="app">
  2. <p ref="pEl">Initial text.</p>
  3. <button v-on:click="changeText">Change text</button>
  4. </div>
  5. <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
  6. <script type="module">
  7. const app = Vue.createApp({
  8. methods: {
  9. changeText(){
  10. this.$refs.pEl.innerHTML = "Hello!";
  11. }
  12. }
  13. })
  14. app.mount('#app')
  15. </script>

定义与用法

ref 属性用于标记 <template> 中的元素,以便可以从 <script> 中的 $refs 对象访问这些元素。

我们可以使用 Vue 中的 ref 属性和 $refs 对象来替代普通 JavaScript 中的方法,如 getElementById()querySelector()

如果使用 v-for 创建的 HTML 元素具有 ref 属性,则生成的 DOM 元素将作为数组添加到 $refs 对象中,如本例所示:

实例
  1. <ul>
  2. <li v-for="x in liTexts" ref="liEl">{{ x }}</li>
  3. </ul>

更多实例

实例 1

<p> 元素内的文本已更改。

  1. <template>
  2. <h1>Example</h1>
  3. <p>Click the button to put "Hello!" as the text in the green p element.</p>
  4. <button @click="changeVal">Change Text</button><br>
  5. <p ref="pEl" id="pEl">This is the initial text</p>
  6. </template>
  7. <script>
  8. export default {
  9. methods: {
  10. changeVal() {
  11. this.$refs.pEl.innerHTML = "Hello!";
  12. }
  13. }
  14. };
  15. </script>
  16. <style>
  17. #pEl {
  18. background-color: lightgreen;
  19. display: inline-block;
  20. }
  21. </style>
实例 2

第一个 <p> 标记中的文本被复制到第二个 <p> 标记中。

  1. <template>
  2. <h1>Example</h1>
  3. <p ref="p1">Click the button to copy this text into the paragraph below.</p>
  4. <button @click="transferText">Transfer text</button>
  5. <p ref="p2">...</p>
  6. </template>
  7. <script>
  8. export default {
  9. methods: {
  10. transferText() {
  11. this.$refs.p2.innerHTML = this.$refs.p1.innerHTML;
  12. }
  13. }
  14. };
  15. </script>
实例 3

<p> 元素获得的内容与输入字段中所写内容相同。

  1. <template>
  2. <h1>Example</h1>
  3. <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>
  4. <input ref="inputEl" @input="getRefs" placeholder="Write something..">
  5. <p ref="pEl"></p>
  6. </template>
  7. <script>
  8. export default {
  9. methods: {
  10. getRefs() {
  11. this.$refs.pEl.innerHTML = this.$refs.inputEl.value;
  12. }
  13. }
  14. };
  15. </script>
实例 4

该按钮显示第三个列表元素,该元素作为数组元素存储在 $refs 对象中。

  1. <template>
  2. <h1>Example</h1>
  3. <p>Click the button to reveal the 3rd list element stored as an array element in the $refs object.</p>
  4. <button @click="getValue">Get the 3rd list element</button><br>
  5. <ul>
  6. <li v-for="x in liTexts" ref="liEl">{{ x }}</li>
  7. </ul>
  8. <pre>{{ thirdEl }}</pre>
  9. </template>
  10. <script>
  11. export default {
  12. data() {
  13. return {
  14. thirdEl: ' ',
  15. liTexts: ['Apple','Banana','Kiwi','Tomato','Lichi']
  16. }
  17. },
  18. methods: {
  19. getValue() {
  20. this.thirdEl = this.$refs.liEl[2].innerHTML;
  21. console.log("this.$refs.liEl = ",this.$refs.liEl);
  22. }
  23. }
  24. };
  25. </script>
  26. <style>
  27. pre {
  28. background-color: lightgreen;
  29. display: inline-block;
  30. }
  31. </style>

分类导航