我是Angular的新手,目前我正在做路由工作。我在组件中使用子路由时遇到了一个问题。routerLink不会在组件中呈现为链接。如果我使用路由器插座,应用程序就会崩溃。
使用路由器插座时,我得到以下错误消息:
src/app/gardening/gardening/gardening.component.html:5:1 - error NG8001: 'router-outlet' is not a known element:
1. If 'router-outlet' is an Angular component, then verify that it is part of this module.
2. If 'router-outlet' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
5 <router-outlet></router-outlet>
~~~~~~~~~~~~~~~
src/app/gardening/gardening/gardening.component.ts:5:16
5 templateUrl: './gardening.component.html',
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Error occurs in the template of component GardeningComponent.我在谷歌上搜索了错误消息,解决方案总是缺少模块中的RouterModule导入。我已经导入了RouterModule。
这是我的module.ts
@NgModule({
declarations: [GardeningComponent, DemogardenComponent],
imports: [
CommonModule, RouterModule
],
exports:[GardeningComponent, DemogardenComponent]
})
export class GardeningModule { }这是我的app.module.ts
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }怎么了,遗漏了什么?
如果您愿意,也可以在github上检查我的存储库中的代码:SimpleRouting
发布于 2021-03-21 00:25:48
我没有看到您提到的完全相同的错误,但我看到了与缺少导入有关的其他问题。
由于您有单独的Home和Gardening模块,因此还需要将它们导入到app.module.ts文件中:
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
GardeningModule, // <------
HomeModule // <------
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }在这两处小改动之后,您的代码可以正常工作。(工作StackBlitz)
发布于 2021-03-21 01:11:33
你的问题是因为应用程序模块不知道你的园艺模块。所以你得到的是最新的router-outlet错误。
以下是解决方案-只需像这样更新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 { GardeningModule } from './gardening/gardening.module'; // <=============
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
GardeningModule // <=============
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }https://stackoverflow.com/questions/66723751
复制相似问题