更改 view/component 但不更改 url

Change view/component but not url

我正在使用 404 错误页面功能,我可以使用它:

const router = new VueRouter({
  routes: [
        // ...
        {
            path: '*',
            component: NotFound,
            name: '404',
            meta: {page_title: 'Vuejs&Material Demo | 404 NOT FOUND'}
        },
    ]
});

// ... not found something

this.$router.push({name: '404'});

上面的问题是 url 也改变了,但我只想改变视图(组件)。

当一个简单的打字错误重定向到 http://example.com/404 时,我觉得很烦人,它让我再次输入 url 或从浏览器自动填充中退出,无论哪种方式我都不喜欢。

我想知道是否有某种 method/logic 有这样的东西:https://www.facebook.com/sdfsd/sfsdf/asfasfsafastgtgregre .

最重要的是,我想要更改视图而不是 url。

有什么vue/vue-router方法可以做到这一点吗?

这有点 hacky,但您可以通过路由器的 history 对象的 updateRoute 方法更新路由:

let route = this.$router.match({ name: '404' });
this.$router.history.updateRoute(route);