简体   繁体   English

TypeError:无法使用 VueJS 读取未定义的属性(读取 '$refs')

[英]TypeError: Cannot read properties of undefined (reading '$refs') with VueJS

Parent Component:父组件:

...
<v-stepper-step
:rules="[()=>isValid(1)]"
>
    MyStep
</v-stepper-step>
<v-stepper-content>
    <Mytag ref="MyReference" />
</v-stepper-content>
...
methods: {
    isValid(number){
      return mySwitch(number)
 }
    mySwitch(number){
      let v = false
      switch(number){
        case '1':
        v = this.$refs.MyReference.$refs.MyTagForm.validate()
        break
      }
     return v
 }
}

Child Component:子组件:

...
<v-form
  ref="MyTagForm"
>
...
</v-form>
...

Following problem: as soon as the page is loaded I get the TypeError: Cannot read properties of undefined (reading '$refs').以下问题:加载页面后,我得到 TypeError: Cannot read properties of undefined (reading '$refs')。 BUT as soon as the page is loaded and I change between steps everything with the validation is fine.但是一旦页面加载并且我在步骤之间进行更改,验证的一切都很好。

I think the problem is that the reference just doesn't exist at the very beginning.我认为问题在于该参考在一开始就不存在。 I tried to use setInterval and setTimeout around the method in isValid and in mySwitch - but then the validation is always false.我尝试在 isValid 和 mySwitch 中的方法周围使用 setInterval 和 setTimeout - 但验证总是错误的。

I hope someone of you can help.我希望你们中的某个人可以提供帮助。

The problem is that in Vue.js, a component is rendered through a particular set of stages--for instance, a component is first created before it is mounted--and you're attempting to retrieve a reference to a rendered DOM object that has not yet been rendered by the framework.问题是在 Vue.js 中,一个组件是通过一组特定的阶段呈现的——例如,一个组件在它被挂载之前首先被创建——并且你试图检索一个对呈现的 DOM object 的引用,它没有尚未由框架呈现。 In other words, you're trying to retrieve something that doesn't yet exist (as you suspected).换句话说,您正在尝试检索尚不存在的东西(正如您所怀疑的那样)。

In order to resolve this problem, you have to account for the child component not yet being rendered, choosing instead to defer validation until the component has been mounted.为了解决这个问题,你必须考虑到子组件还没有被渲染,而是选择推迟验证直到组件被挂载。 One such option would be to have the child component signal to the parent when it has successfully mounted and return a default validation value of true or false until then.一种这样的选择是让子组件在成功安装后向父组件发出信号,并在此之前返回默认验证值truefalse This example should illustrate the idea:这个例子应该说明这个想法:

...
<v-stepper-step
:rules="[()=>isValid(1)]"
>
    MyStep
</v-stepper-step>
<v-stepper-content>
    <Mytag ref="MyReference" v-on:ready="childReady = true" />
</v-stepper-content>

{
  ...
  data() {
    return {
      childReady: false
    }
  },
  methods: {
    isValid(number){
      return mySwitch(number)
  }
  mySwitch(number){
    // Prevents the normal switch code from running until the child component mounts.
    if(!this.childReady) {
      return false;
    }

    let v = false
    switch(number){
      case '1':
      v = this.$refs.MyReference.$refs.MyTagForm.validate()
      break
    }
    return v
  }
}
...
<v-form
  ref="MyTagForm"
>
...
</v-form>
...

{
  ...
  mounted() {
    // The mounted lifecycle hook will signal the "ready" event when this component is rendered, allowing the parent to know that this component has finished rendering.
    this.$emit('ready');
  }
}

There are, of course, other ways that you can manage this, and I would argue that there are multiple much better solutions than the one I've illustrated above.当然,还有其他方法可以解决这个问题,我认为有多个比我上面说明的解决方案更好的解决方案。 That said, I wanted to keep this answer simple;也就是说,我想让这个答案保持简单; the above should hopefully be sufficient for aiding you in grasping what the underlying problem is and giving you the starting point for better designing the solution you want in your code.希望以上内容足以帮助您掌握根本问题,并为您提供更好地设计代码中所需解决方案的起点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Vue.js:“TypeError:无法读取未定义的属性(读取'$refs)'” - Vue.js: "TypeError: Cannot read properties of undefined (reading '$refs)'" TypeError:无法读取未定义的属性(读取“集合”)-Vuejs 和 Firebase - TypeError: Cannot read properties of undefined (reading 'collection') - Vuejs and Firebase vuejs 2 组合 api: TypeError: Cannot read properties of undefined (reading 'length') - vuejs 2 composition api: TypeError: Cannot read properties of undefined (reading 'length') VueJS TypeError:无法读取未定义的属性(读取'toLowerCase') - VueJS TypeError: Cannot read properties of undefined (reading 'toLowerCase') 类型错误:无法读取未定义的属性(读取“$router”)vuejs - TypeError: Cannot read properties of undefined (reading '$router') vuejs “TypeError”:无法读取 Vuejs 中未定义的属性? - "TypeError": Cannot read properties of undefined in Vuejs? 无法读取未定义的属性(读取“$set”)vuejs - Cannot read properties of undefined (reading '$set') vuejs 类型错误:无法读取未定义的属性(读取“createdTimestamp”) - TypeError: Cannot read properties of undefined (reading 'createdTimestamp') TypeError:无法读取未定义的属性(读取“forEach”) - TypeError: Cannot read properties of undefined (reading 'forEach') TypeError:无法读取未定义的属性(读取“学生”) - TypeError: Cannot read properties of undefined (reading 'students')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM