问题
在项目中用了延时
data() {
return {
timer:""
};
},
created() {
let _this = this;
_this.timer = setTimeout(() => {
//自己的代码
}
}, 1000);
},
destroyed () {
let _this = this;
clearTimeout(_this.timer) // 清除
}
然鹅,发现每次只执行了一次。页面刚渲染的时候是正确的,切换路由就给人一种延时器没有清除的感觉。
解决办法
额,后来发现不是定时器的问题。是因为父子组件的加载顺序为
父beforeCreated ->父created ->父beforeMounted ->子beforeCreated ->子created ->子beforeMounted ->子mounted -> 父mounted
然鹅,虽然程序是按这个顺序写的,但是因为子组件自身在created阶段执行可能较费时间,而父组件mounted的时候需要子组件created后的某些值。所以就在父组件mounted的时候,给方法加了延时1秒。目前解决了问题。
子组件内
父组件内