【Vue】报错:this.$refs 引用子组件报错 is not a function

文章目录

报错信息

  • this.$refs.selectTree.onHide is not a function
    在这里插入图片描述

解决方法

  1. 确保组件成功导出并挂载:
- 子组件需要 import,import 是请确保路径正确
	import selectTree from '@/components/select-tree';
- import 之后还需要在父组件的 component 中进行注册
	components: {
    
     selectTree }
  1. 打印下this.$refs.selectTree,看下他是否能正常获取到和他的返回格式

在这里插入图片描述

// 如上:上面的返回的是一个数组,所以我们应该这样用
this.$refs.selectTree[0].onHide();
  1. 如果打印this.$refs.selectTreeundefined,则检查ref绑定是否正常(区分大小写)
<select-tree ref = "selectTree"/>
// 错误示例:不分大小写
console.log(this.$refs.SelectTree)	// undefined
// 正确示例:区分大小写
console.log(this.$refs.selectTree)	// [VueComponent]
  1. 最后就是确保 子组件 里面存在这个方法
console.log(this.$refs.selectTree)

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_45677671/article/details/131762312