Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

angular - 'router-outlet' is not a known element while executing ng build --prod

I am getting below error while doing ng build --prod. It works fine in ng build.

Error: src/app/app.component.html:1: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.
1 <router-outlet></router-outlet>
  ~~~~~~~~~~~~~~~

  src/app/app.component.ts:5:16
    5   templateUrl: './app.component.html',
                     ~~~~~~~~~~~~~~~~~~~~~~
    Error occurs in the template of component AppComponent.

I am using RouterModule in app.module.ts file

import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule } from  '@angular/common/http';
import { NgModule,CUSTOM_ELEMENTS_SCHEMA,NO_ERRORS_SCHEMA  } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import {FormsModule } from '@angular/forms';
import {NgbDropdown, NgbModule} from '@ng-bootstrap/ng-bootstrap';
import { FilterPipe } from './dashboard/filter.pipe';

@NgModule({
  declarations: [
    AppComponent,
    DashboardComponent,
    TimeAgoPipe,
    LastseenDevicesComponent,
    FilterPipe
  ],
  imports: [
    HttpClientModule,
    FormsModule,
    RouterModule,
    AppRoutingModule, 
    BrowserModule,
    BrowserAnimationsModule,
  ],
  exports: [],
  entryComponents: [DevicesComponent],
  bootstrap: [ AppComponent ],
  providers: [ NgbDropdown],
  schemas: [ CUSTOM_ELEMENTS_SCHEMA,NO_ERRORS_SCHEMA]
})
export class AppModule { }

app-routing.module.ts file

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { DashboardComponent } from './dashboard/dashboard.component';
 
const routes: Routes = [
 
 {
 path: '', pathMatch: 'full' ,component: DashboardComponent,  
  },
 
];
 
@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule],
})
export class AppRoutingModule { }

How can I fix this ?

Angular CLI: 10.0.5 Node: 12.18.3

question from:https://stackoverflow.com/questions/65844526/router-outlet-is-not-a-known-element-while-executing-ng-build-prod

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your AppRoutingModule should already import and export the RouterModule. So you should remove RouterModule from your AppModule imports.

I assume your AppRoutingModule looks something like this:

const appRoutes: Route[] = [...]

@NgModule({
 imports: [RouterModule.forRoot(appRoutes)],
 exports: [RouterModule]
})
export class AppRoutingModule {}

On a sidenote I also highly discourage the use of NO_ERRORS_SCHEMA


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...