Angular路由配置报错:router-outlet不是已知元素的解决求助
解决Angular中“router-outlet不是已知元素”的错误
出现这个错误的核心原因是Angular无法识别路由相关指令,以下是针对性的解决步骤:
确保AppRoutingModule已导入根模块
你的路由模块配置本身没问题,但必须在AppModule中导入它,才能让Angular加载路由指令。打开app.module.ts,确认代码包含以下部分:import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; // 声明路由中用到的组件 import { LoginComponent } from './components/pages/login/login.component'; import { BeszerzesEszkozSelectorComponent } from './components/pages/beszerzes-eszkoz-selector/beszerzes-eszkoz-selector.component'; @NgModule({ declarations: [ AppComponent, LoginComponent, BeszerzesEszkozSelectorComponent ], imports: [ BrowserModule, AppRoutingModule // 这行必须存在,用于导入路由指令 ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }检查组件的声明状态
路由中引用的LoginComponent和BeszerzesEszkozSelectorComponent必须在当前模块(或已导入的特性模块)的declarations数组中,否则Angular无法关联路由与组件。重置开发服务器缓存
若上述配置都正确但错误依然存在,清理缓存并重启服务器:ng serve --delete-output-path
内容的提问来源于stack exchange,提问作者Callistron
相关产品推荐
相关产品推荐

