在计算属性中使用 $refs [英] Using $refs in a computed property

查看:51
本文介绍了在计算属性中使用 $refs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何访问计算内部的 $refs?第一次运行计算属性时总是未定义的.

How do I access $refs inside computed? It's always undefined the first time the computed property is run.

推荐答案

要在这里回答我自己的问题,我在其他任何地方都找不到满意的答案.有时你只需要访问一个 dom 元素来进行一些计算.希望这对其他人有帮助.

Going to answer my own question here, I couldn't find a satisfactory answer anywhere else. Sometimes you just need access to a dom element to make some calculations. Hopefully this is helpful to others.

一旦组件被挂载,我不得不欺骗 Vue 更新计算属性.

I had to trick Vue to update the computed property once the component was mounted.

Vue.component('my-component', {
  data(){
    return {
      isMounted: false
    }
  },
  computed:{
    property(){
      if(!this.isMounted)
        return;
      // this.$refs is available
    }
  },
  mounted(){
    this.isMounted = true;
  }
})

这篇关于在计算属性中使用 $refs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆