返回
修复 Vue 3.0 路由错误:"No active route record was found"
前端
2023-10-05 03:34:06
Vue 3.0 路由报错:"No active route record was found. Are you missing a
在 Vue 3.0 中,如果你遇到 "No active route record was found. Are you missing a
-
你没有在应用程序中包含
<router-view>
组件:<router-view>
组件是 Vue Router 用于渲染路由视图的地方。如果你没有在应用程序中包含它,Vue 将无法找到要渲染的内容。 -
你正在使用过时的路由监测方式: 在 Vue 2.0 中,路由监测是通过
watch
选项进行的。然而,在 Vue 3.0 中,路由监测已更改为onBeforeRouteUpdate
守卫。如果你正在使用过时的路由监测方式,你将需要将其更新为onBeforeRouteUpdate
。
修复该错误
要修复此错误,请执行以下步骤:
- 确保你已在应用程序中包含
<router-view>
组件: 在你的App.vue
文件中,确保你包含了<router-view>
组件。它应该是这样的:
<template>
<div id="app">
<router-view />
</div>
</template>
- 更新到新的路由监测方式: 如果你正在使用过时的路由监测方式,请将其更新为
onBeforeRouteUpdate
守卫。以下是onBeforeRouteUpdate
的示例:
import { onBeforeRouteUpdate } from 'vue-router'
export default {
setup() {
onBeforeRouteUpdate((to, from) => {
// 在路由更新之前执行某些操作
})
}
}
- 删除旧的路由监测方式: 如果你在应用程序中同时使用
watch
和onBeforeRouteUpdate
来监测路由,请删除旧的watch
选项。
结论
通过遵循这些步骤,你应该能够修复 Vue 3.0 中的 "No active route record was found. Are you missing a