问题

       使用element制作标签页时,需要根据页面当前的url设置标签页的初始状态,所以需要在mounted中获取当前的路由信息,代码如下:

mounted() {
	console.log(this.$route);		// 无法打印出正常的url信息
}

原因

       由于vue中的dom渲染过程,我们无法在mounted中获取dom信息,也就没有办法获取当前路由的信息。

解决办法

       使用setTimeout()方法,延迟获取dom及获取url信息的操作,代码如下:

mounted() {
	let _this = this;
	setTimeout(function() {
		console.log(_this.$route);
	}, 100);
}

       这里需要注意,在setTimeout()中的this和mounted中的this不是同一个this,所以需要通过变量(_this)在setTimeout()中获取$route。

Logo

前往低代码交流专区

更多推荐