vue父页面调用子组件的方法报错:Error in mounted hook: “TypeError: Cannot read property ‘aa’ of undefined”
七娃博客962人阅读
vue父页面调用子组件的方法报错:Error in mounted hook: "TypeError: Cannot read property 'aa' of undefined"
出现原因:数据加载的时候组件还没有渲染,因此是获取不到组件的名字,所以 this.$refs.childs 会报错!在加载数据的时候当然是 未定义了。
解决办法:异步加载,用定时器延迟事件!
setTimeout(() => { this.$refs.childs.resets() // 调用子组件的方法,重置分页 }, 10)
注意:setTimeout会改变函数的this指向,但是ES6的指针函数不会改变,当然也可以用下面的写法:
let that = this setTimeout(function () { that.$refs.childs.resets() // 调用子组件的方法,重置分页 }, 10)
同样能解决问题,setTimeout的这个知识点不注意,可能还会报错:Uncaught TypeError: Cannot read property 'childs' of undefined......
[…] 注意:一定要加计时器!!!不然会报错:Error in mounted hook: “TypeError: Cannot read property ‘***’ of undefined” […]