router-outlet is not a known element

做~自己de王妃 提交于 2019-12-04 22:50:51
Gerard

Like Erion said: In my case I did (added) two things in app.component.spec.ts:

import { RouterTestingModule } from '@angular/router/testing';
...
TestBed.configureTestingModule({ 
  imports: [ RouterTestingModule ],
  declarations: [
    AppComponent
  ],
}).compileComponents();
Erion .Dreyer

Probably I'm late here, but since I got the exaclty same issue and found an answer here.

Are you using angular-cli? https://github.com/angular/angular-cli/pull/3252/files#diff-badc0de0550cb14f40374a6074eb2a8b

In my case I just had to import my new router module in the app.component.spec.ts The js import and NgModule import.

Then restart the test server.

The angular router directive is part of part of lib: @angular/router and it acts a placeholder which is filled up by angular state.

Here I am dividing two file separately i.e. app.module.ts and app.router.ts

Here app.router.ts

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { MainComponent } from './components/main/main.component';
import { AppComponent } from './app.component';

const appRoutes: Routes = [
  { path: 'main', component: MainComponent },
  { path: '', redirectTo: '/main', pathMatch: 'full' }
];
@NgModule({
  imports: [
    RouterModule.forRoot(appRoutes)
  ],
  exports: [
    RouterModule
  ]
})
export class AppRouter { }

Here app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRouter } from './app.router';
import { AppComponent } from './app.component';
import { MainComponent } from './components/main/main.component';

@NgModule({
  declarations: [
    AppComponent,
    MainComponent
  ],
  imports: [
    BrowserModule,
    AppRouter
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Now you can use <router-outlet></router-outlet> in your app.component.ts file and won't show any error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!