There is a discussion in github: change current component used by router-view without changing current URL
However, the expecting function replaceView is still unavailable so far.
I have the exactly same requirement as yours, and I find a workaround:
I have a route component called App.vue (I use Quasar Framework). Its template looks like:
<template>
<div id="q-app">
<div v-if="$store.state.g.errorState">
<the-error-page>Error Message</the-error-page>
</div>
<router-view v-else />
</div>
</template>
I use vuex's global store state.g.errorState to flag the error state. If it is true, display the error page, otherwise display the normal router view.
If I have a error in my page, I just commit the true value to state.g.errorState:
<script>
export default {
data() {
// ...
},
beforeMount() {
if (somethingWrong) {
this.$store.commit('g/errorState', true)
}
}
}
</script>
Obviously, the state.g.errorState value should be restored to false every time the route changes:
const Router = new VueRouter({
// ...
})
Router.beforeEach((to, from, next) => {
store.commit('g/errorState', false)
next()
})