今天在写博客系统时,要删除某篇博客,删除了以后用路由进行到该页跳转,但是毫无结果,算是自己踩过的一个坑吧。
首先:先说明一下一共有四种方法。(首先排除路由重新跳转到该页面,发现毫无作用)
1: location. reload()
2:this.$router.go(0)
这两种都可以刷新当前页面的,缺点就是相当于按ctrl+F5 强制刷新那种,整个页面重新加载,会出现一个瞬间的空白页面,体验不好
3:新建一个空白页面,先跳转到空白页面,在跳转回来,不过此时地址栏将会发生快速变化,其实也是不太实用的。
最好用的还是第四种
4:provide / inject 组合 方式是我试过最实用的
(1)首先改变App.vue页面,该代码课直接套用,就算不用页面刷新也没影响
<template>
<div id="app">
<router-view v-if="isRouterAlive"></router-view>
</div>
</template>
<script>
export default {
name: 'app',
provide(){
return{
reload: this.reload
}
},
data(){
return{
isRouterAlive: true
}
},
methods:{
reload(){
this.isRouterAlive = false;
this.$nextTick(function (){
this.isRouterAlive = true;
})
}
}
}
</script>
<style>
</style>
(2):在要跳转的页面加上 inject:["reload"],所要加入的位置是
(3)调用时直接使用 this.reload();