Vue.js 使用nextTick()时$ref未定义-vuejs

Vue.js 使用nextTick()时$ref未定义-vuejs,vue.js,vuejs2,Vue.js,Vuejs2,我正在使用eventListener获取我的两个元素h1和wrap的scrollHeight 我遇到的问题是,当组件装载时,尽管使用了nextTick,它们仍显示为undefined。只有在用户调整大小后,才会显示正确的值 如何获得装载时h1和wrap的滚动高度 data() { return { titleIsExpanded: true, shouldShowArrow: null, }; }, async getWindowWidth() {

我正在使用eventListener获取我的两个元素
h1
wrap
scrollHeight

我遇到的问题是,当组件装载时,尽管使用了
nextTick
,它们仍显示为
undefined
。只有在用户调整大小后,才会显示正确的值

如何获得装载时
h1
wrap
的滚动高度

data() {
    return { 
      titleIsExpanded: true,
      shouldShowArrow: null,
    };
  },
async getWindowWidth() {
      await this.$nextTick();
      console.log(wrap)
      console.log(h1)
      const h1 = this.$refs.h1.scrollHeight;
      const wrap = this.$refs.wrap.scrollHeight;
  
      if(h1 > wrap) { 
        this.shouldShowArrow = true;
      } else {
        this.shouldShowArrow = false;
      }
    },
  },
  async mounted() {
    await this.$nextTick();
    this.getWindowWidth();
    window.addEventListener('resize', () => { this.getWindowWidth(); });
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.getWindowWidth() );
  },

我认为您必须将“getWindowWidth”函数包装在方法部分中

data(){
 return{
  ....
 }
},
methods:{
  async getWindowWidth(){
   ...
  }
}