location.reload()
this.$router.go(0)
1.代码简单,仅需一行代码即可实现效果;
2.便于理解,相当于重新加载一遍系统;
相当于按ctrl+F5 强制刷新,整个页面将重新加载,故而加载速度慢,会出现一个瞬间的空白页面,体验感不好。
this.$router.replace({
path: this.$route.path,
query: {
routeKey: new Date().getTime()
}
})
watch: {
'$route'(newVal) {
if(newVal.query.routeKey) {
// 调用需变更的方法
}
}
}
代码简单;
this.$router.replace({
path: '/centerPage',
query: {to:this.$route.path}
})
<template>
<div class='centerPage'></div>
</template>
<script>
export default {
name: "centerPage",
methods: {
jump() {
let url = this.$route.query.to
this.$router.replace(url)
}
},
created() {
this.jump()
}
}
</script>
1.不会像方案一样有长时间的白屏;
2.多个需求需要时,可重复使用;
provide/inject 是 Vue 在 2.2.0 版本新增的 API;
provide 可以在祖先组件中指定我们想要提供给后代组件的数据或方法,而在任何后代组件中,我们都可以使用 inject 来接收 provide 提供的数据或方法。
<template>
<div id="app">
<router-view v-if='isRouterAlive'/>
</div>
</template>
<script>
export default {
name: 'App',
components: {
},
data() {
return{
isRouterAlive: true // 控制页面显隐
}
},
provide() {
return{
reload: this.reload
}
},
methods: {
reload(){
this.isRouterAlive = false
this.$nextTick(() => {
this.isRouterAlive = true
})
}
}
}
</script>
<template>
<div class='currentPage'></div>
</template>
<script>
export default {
name: "currentPage",
inject: ['reload'], // 引用
methods: {
submit() {
this.reload() // 调用
}
}
}
</script>
因篇幅问题不能全部显示,请点此查看更多更全内容