首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular 11 - RouterModule - 'router-outlet‘不是已知元素

Angular 11 - RouterModule - 'router-outlet‘不是已知元素
EN

Stack Overflow用户
提问于 2021-03-21 00:11:47
回答 2查看 2K关注 0票数 2

我是Angular的新手,目前我正在做路由工作。我在组件中使用子路由时遇到了一个问题。routerLink不会在组件中呈现为链接。如果我使用路由器插座,应用程序就会崩溃。

使用路由器插座时,我得到以下错误消息:

代码语言:javascript
复制
    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

代码语言:javascript
复制
@NgModule({
declarations: [GardeningComponent, DemogardenComponent],
imports: [
CommonModule, RouterModule
],
exports:[GardeningComponent, DemogardenComponent]

})
export class GardeningModule { }

这是我的app.module.ts

代码语言:javascript
复制
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }

怎么了,遗漏了什么?

如果您愿意,也可以在github上检查我的存储库中的代码:SimpleRouting

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-03-21 00:25:48

我没有看到您提到的完全相同的错误,但我看到了与缺少导入有关的其他问题。

由于您有单独的HomeGardening模块,因此还需要将它们导入到app.module.ts文件中:

代码语言:javascript
复制
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,   
    GardeningModule,    // <------
    HomeModule          // <------
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

在这两处小改动之后,您的代码可以正常工作。(工作StackBlitz)

票数 3
EN

Stack Overflow用户

发布于 2021-03-21 01:11:33

你的问题是因为应用程序模块不知道你的园艺模块。所以你得到的是最新的router-outlet错误。

以下是解决方案-只需像这样更新app.module.ts文件:

代码语言:javascript
复制
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 { }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66723751

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档