mounted() {
let start = window.location.href.lastIndexOf("/");
let path = window.location.href.slice(start + 1);
if (path == "path1") {
console.log("path1");
this.getmessage("admin");
} else {
console.log("path2");
this.getmessage("student");
}
}
1. 切换页面,不会重新发送请求
方法
watch $router
watch: {
$route(to, from) {
console.log("路由变化", to.path);
if (to.path == "/practice-center/path1") {
this.getmessage("admin");
} else {
this.getmessage("student");
}
}
},
2.切换页面,数据不是初始状态
方法
- 将data数据在watch router重新设置一份(数据量大,不推荐)
-
<router-view :key="$route.path"/>
要保证key 的唯一性