- 页面异常捕获
请求失败或页面找不到调用$throw方法
const errorHandler = (error) => {
var vm = new Vue({
router
});
console.error('抛出异常');
vm.$router.replace('/404');
}
function $throw(error) {
errorHandler(error)
}
<template>
<div id='bg' class="bg" v-show="showFlag">
<div class="container">
<p>页面开小差了</p>
<p class="btn" @click='refresh'>点击刷新</p>
</div>
</div>
</template>
<script>
export default {
data(){
return{
showFlag: true,
path:''
}
},
inject:['reload'],
mounted() {
},
beforeRouteEnter (to, from, next) {
next(vm => {
// 通过 `vm` 访问组件实例
vm.path=to.redirectedFrom||from.fullPath;//地址错误或请求失败
console.log( vm.path)
})
},
methods:{
refresh(){
this.$router.replace(this.path)
}
}
}
</script>