Vue---- ref 引用


ref 引用

1. ref 引用

ref 用来辅助开发者在不依赖于 jQuery 的情况下,获取 DOM 元素或组件的引用。

每个 vue 的组件实例上,都包含一个 $refs对象,里面存储着对应的 DOM 元素或组件的引用。默认情况下,组件的 $refs指向一个空对象。

<template>
  <div>
    <h1>App 组件</h1>
    <hr>
    <button @click="getRef"> 获取 $ref </button>
  </div>
</template>

<script>
export default {
  name: 'App',
  methods: {
    getRef() {
      console.log( this )
    }
  }
}
</script>

<style>

</style>

请添加图片描述
请添加图片描述

<template>
  <div>
    <h1>App 组件</h1>
    <hr>
    <button @click="getRef"> 获取 $ref </button>
  </div>
</template>

<script>
export default {
  name: 'App',
  methods: {
    getRef() {
      console.log( this.$refs )
    }
  }
}
</script>

<style>

</style>

请添加图片描述
请添加图片描述

2. 使用 ref 引用 DOM 元素

使用 ref 引用页面上的 DOM 元素。

<template>
  <div>
    <!-- 使用 ref 属性,为对应的 DOM 元素添加引用名称 -->
    <h1 ref="myh1">App 组件</h1>
    <hr>
    <button @click="getRef"> 获取 $ref </button>
    <br>
    <button @click="change"> App change </button>
  </div>
</template>

<script>
export default {
  name: 'App',
  methods: {
    getRef() {
      console.log( this.$refs )
    },
    change() {
	  // 通过 this.$refs.引用名称 获取对应 DOM 元素的引用
      console.log( this.$refs.myh1 )
      // 改变 h1 的字体颜色
      this.$refs.myh1.style.color = 'red'
    }
  }
}
</script>

<style>

</style>

请添加图片描述
请添加图片描述
请添加图片描述

3. 使用 ref 引用组件实例

使用 ref 引用页面上的组件实例。

与 DOM 元素一样,使用 ref 属性,为对应的组件添加引用名称。通过 this.$refs.引用名称 获取对应组件的引用。

<template>
  <div>
    <h3>Count 组件</h3>
    <div> {{count}} </div>
  </div>
</template>

<script>
export default {
  name: 'Count',
  data() {
    return {
      count: 0
    }
  },
  methods: {
    add() {
      this.count++
    }
  }
}
</script>

<style>

</style>
<template>
  <div>
    <!-- 使用 ref 属性,为对应的 DOM 元素添加引用名称 -->
    <h1 ref="myh1">App 组件</h1>
    <hr>
    <button @click="getMyCount"> get myCount </button>
    <Count ref="myCount"></Count>
  </div>
</template>

<script>
import Count from './Count.vue'

export default {
  name: 'App',
  methods: {
    getMyCount() {
      console.log( this.$refs.myCount )
    }
  },
  components: {
    Count
  }
}
</script>

<style>

</style>

请添加图片描述请添加图片描述

调用组件内定义的方法

<template>
  <div>
    <!-- 使用 ref 属性,为对应的 DOM 元素添加引用名称 -->
    <h1 ref="myh1">App 组件</h1>
    <hr>
    <button @click="getMyCount"> get myCount </button>
    <button @click="countAdd"> count +1 </button>
    <Count ref="myCount"></Count>
  </div>
</template>

<script>
import Count from './Count.vue'

export default {
  name: 'App',
  methods: {
    getMyCount() {
      console.log( this.$refs.myCount )
    },
    countAdd() {
      // 调用组件内定义的 add 方法
      this.$refs.myCount.add()
    }
  },
  components: {
    Count
  }
}
</script>

<style>

</style>

请添加图片描述
请添加图片描述

4. 控制文本框和按钮的按需切换 & 让文本框自动获得焦点

通过布尔值 inputVisible 来控制组件中的文本框与按钮的按需切换。

当文本框展示出来之后,如果希望它立即获得焦点,则可以为其添加 ref 引用,并调用原生 DOM 对象的
.focus() 方法即可。

<template>
  <div>
    <!-- 使用 ref 属性,为对应的 DOM 元素添加引用名称 -->
    <h1 ref="myh1">App 组件</h1>
    <hr>
    <input type="text" v-show="inputVisible" ref="myipt">
    <button @click="ipt_change">ipt change</button>
  </div>
</template>

<script>
import Count from './Count.vue'

export default {
  name: 'App',
  data() {
    return {
      inputVisible: false
    }
  },
  methods: {
    ipt_change() {
      this.inputVisible = !this.inputVisible
      this.$refs.myipt.focus()
    }
  },
  components: {
    Count
  }
}
</script>

<style>

</style>

此时切换输入框的显示状态,不能实现聚焦。因为this.$refs.myipt.focus()执行时候,组件还未显示在页面上。所以无法实现聚焦,this.$refs.myipt.focus()的执行速度快于元素的显示。

请添加图片描述
请添加图片描述

5. this.$nextTick(cb) 方法

组件的 $nextTick(cb) 方法,会把 cb 回调推迟到下一个 DOM 更新周期之后执行。

通俗的理解是:
等组件的DOM 异步地重新渲染完成后,再执行 cb 回调函数。从而能保证 cb 回调函数可以操作到最新的 DOM 元素。

对上述代码进行修改:

  methods: {
    ipt_change() {
      this.inputVisible = !this.inputVisible
      //组件的DOM 异步地重新渲染完成后,再执行 cb 回调函数。
      this.$nextTick( ()=>{
        this.$refs.myipt.focus()
      } )
    }
  },
<template>
  <div>
    <!-- 使用 ref 属性,为对应的 DOM 元素添加引用名称 -->
    <h1 ref="myh1">App 组件</h1>
    <hr>
    <input type="text" v-show="inputVisible" ref="myipt">
    <button @click="ipt_change">ipt change</button>
  </div>
</template>

<script>
import Count from './Count.vue'

export default {
  name: 'App',
  data() {
    return {
      inputVisible: false
    }
  },
  methods: {
    ipt_change() {
      this.inputVisible = !this.inputVisible
      this.$nextTick( ()=>{
        this.$refs.myipt.focus()
      } )
    }
  },
  components: {
    Count
  }
}
</script>

<style>

</style>

请添加图片描述
请添加图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值