vuejs2 - How to properly handle non-existent named route?
问题描述
I am using the Vue Router in my project. When i link to a route whose name does not exist, so for example:
<router-link :to="{ name: 'test-route' }"> link </router-link>
Vue complains in the console saying:
[vue-router] Route with name 'test-route' does not exist
But nothing happens to the user to indicate that something is wrong. The address bar gets changed to the root route (just /
), but without the homepage actually showing, and my router.onError
handler (which usually handles trying to navigate to lazy loaded components when you lose connection) doesn't fire in this case.
I want to show on screen that something is wrong, a redirection to my 404 page if nothing else.
解决方案
我的建议 - 如果链接无效,则不允许点击链接,您可以在v-on
指令中设置条件。
v-on="{ to: <does link exist (access router)> ? { name: 'test-route' } : null}"
.
可能需要一些语法修复,但这是一般的想法。
推荐阅读
- regex - 无法使用 Groovy 从文本文件中捕获所需的字符串 - Jmeter JSR223
- c# - 阻止 Nuget 将相对路径添加到 .csproj 文件
- python - 我想阻止 Discord Bot 运行
- google-apps-script - 尝试使用工作表作为 Excel 附件发送电子邮件时显示异常错误
- android - 如何允许访问我的网络之外的本地主机 wamp 服务器?
- c# - 找到对象并比较后获取值
- javascript - Javascript - 动态变量
- reactjs - 尝试使用 Tailwind 启动 React 应用程序失败
- pine-script - Pine 脚本 - 动态历史参考
- bash - 列出不遵循特定格式的目录