IT TIP

'router-outlet'은 알려진 요소가 아닙니다.

itqueen 2020. 12. 29. 08:11
반응형

'router-outlet'은 알려진 요소가 아닙니다.


각도 프런트 엔드가있는 mvc 5 프로젝트가 있습니다. 이 튜토리얼 https://angular.io/guide/router에 설명 된대로 라우팅을 추가하고 싶었습니다 . 그래서 내 _Layout.cshtml에서

<base href="/">

내 app.module에서 내 라우팅을 만들었습니다. 그러나 이것을 실행하면 다음과 같은 오류가 발생합니다.

Error: Template parse errors:
    '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. ("
    <a routerLink="/dashboard">dashboard</a>
    </nav>
    [ERROR ->]<router-outlet></router-outlet>
     "): ng:///AppModule/AppComponent.html@5:0

내 app.component에서 라인

<router-outlet></router-outlet>

Visual Studio에서 'router-outlet'태그를 확인할 수 없다는 오류가 표시됩니다. 이 오류를 해결할 수있는 방법이 있습니까? 참조 또는 가져 오기가 누락되었거나 간과하고 있습니까?

아래는 내 package.json, app.component 및 app.module입니다.

package.json

{
  "version": "1.0.0",
  "name": "app",
  "private": true,
  "scripts": {},
  "dependencies": {
    "@angular/common": "^4.2.2",
    "@angular/compiler": "^4.2.2",
    "@angular/core": "^4.2.2",
    "@angular/forms": "^4.2.2",
    "@angular/http": "^4.2.2",
    "@angular/platform-browser": "^4.2.2",
    "@angular/platform-browser-dynamic": "^4.2.2",
    "@angular/router": "^4.2.2",
    "@types/core-js": "^0.9.41",
    "angular-in-memory-web-api": "^0.3.2",
    "bootstrap": "^3.3.7",
    "core-js": "^2.4.1",
    "graceful-fs": "^4.0.0",
    "ie-shim": "^0.1.0",
    "minimatch": "^3.0.4",
    "reflect-metadata": "^0.1.10",
    "rxjs": "^5.0.1",
    "systemjs": "^0.20.12",
    "zone.js": "^0.8.12"
  },
  "devDependencies": {
    "gulp": "^3.9.1",
    "gulp-clean": "^0.3.2",
    "gulp-concat": "^2.6.1",
    "gulp-tsc": "^1.3.2",
    "gulp-typescript": "^3.1.7",
    "path": "^0.12.7",
    "typescript": "^2.3.3"
  }
}

app.module :

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { RouterModule, Routes } from '@angular/router';

import { AppComponent } from './app.component';
import {DashboardComponent} from "./dashboard/dashboard.component"    

const appRoutes: Routes = [
{
    path: '',
    redirectTo: '/dashboard',
    pathMatch: 'full',
    component: DashboardComponent
},  
{
    path: 'dashboard',
    component: DashboardComponent
}
];
@NgModule({
imports: [
    RouterModule.forRoot(appRoutes),
    BrowserModule,
    FormsModule               
],
exports: [RouterModule],
declarations: [
    AppComponent,  
    DashboardComponent      
],
bootstrap: [AppComponent]
})
export class AppModule {

}

app.component :

import { Component } from '@angular/core';

@Component({
selector: 'my-app',
template: `
          <h1>{{title}}</h1>
          <nav>
          <a routerLink="/dashboard">dashboard</a>
          </nav>
          <router-outlet></router-outlet>
          `
})
export class AppComponent {
    title = 'app Loaded';

}

시도해보십시오 :

@NgModule({
imports: [
    BrowserModule,
    RouterModule.forRoot(appRoutes),
    FormsModule               
],
declarations: [
    AppComponent,  
    DashboardComponent      
],
bootstrap: [AppComponent]
})
export class AppModule {

}

의 수출을 configurate 할 필요가 없습니다 AppModule때문에, AppModule습관이 응용 프로그램의 다른 모듈에서 가져가.


이 시도:

가져 오기 RouterModule에 당신의app.module.ts

import { RouterModule } from '@angular/router';

추가 RouterModuleimports []

이렇게 :

 imports: [    RouterModule,  ]

단위 테스트를 수행하고이 오류가 발생하면 RouterTestingModuleapp.component.spec.ts 또는 추천 구성 요소의 spec.ts 내부로 가져 오기

import { RouterTestingModule } from '@angular/router/testing';

Add RouterTestingModule into your imports [] like 

describe('AppComponent', () => {

  beforeEach(async(() => {    
    TestBed.configureTestingModule({    
      imports: [    
        RouterTestingModule    
      ],
      declarations: [    
        AppComponent    
      ],    
    }).compileComponents();    
  }));

Angular 6을 angular-cli와 함께 사용하고 라우팅 활동을 담당하는 별도의 라우팅 모듈을 만들었다 고 가정하고 Routes 배열에서 경로를 구성합니다. 코드는 다음과 같습니다.

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

app.module.ts에 다음 코드를 추가하면 나를 위해 작동합니다.

@NgModule({
...,
   imports: [
     AppRoutingModule
    ],
...
})

올바른 정의를 찾은 Hero Editor 예제 감사합니다.

앱 라우팅 모듈을 생성 할 때 :

ng generate module app-routing --flat --module=app

app-routing.ts 파일을 업데이트하여 다음을 추가하십시오.

@NgModule({
  imports: [ RouterModule.forRoot(routes) ],
  exports: [ RouterModule ]
})

전체 예는 다음과 같습니다.

import { NgModule }             from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { DashboardComponent }   from './dashboard/dashboard.component';
import { HeroesComponent }      from './heroes/heroes.component';
import { HeroDetailComponent }  from './hero-detail/hero-detail.component';

const routes: Routes = [
  { path: '', redirectTo: '/dashboard', pathMatch: 'full' },
  { path: 'dashboard', component: DashboardComponent },
  { path: 'detail/:id', component: HeroDetailComponent },
  { path: 'heroes', component: HeroesComponent }
];

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

AppRoutingModule을 app.module.ts 가져 오기에 추가하십시오.

@NgModule({
  declarations: [
    AppComponent,
    ...
  ],
  imports: [
    BrowserModule,
    FormsModule,
    AppRoutingModule
  ],
  providers: [...],
  bootstrap: [AppComponent]
})

Its just better to create a routing component that would handle all your routes! From the angular website documentation! That's good practice!

ng generate module app-routing --flat --module=app

The above CLI generates a routing module and adds to your app module, all you need to do from the generated component is to declare your routes, also don't forget to add this:

exports: [
    RouterModule
  ],

to your ng-module decorator as it doesn't come with the generated app-routing module by default!


This issue was with me also. Simple trick for it.

 @NgModule({
  imports: [
   .....       
  ],
 declarations: [
  ......
 ],

 providers: [...],
 bootstrap: [...]
 })

use it as in above order.first imports then declarations.It worked for me.


In your app.module.ts file

import { RouterModule, Routes } from '@angular/router';

const appRoutes: Routes = [
{
    path: '',
    redirectTo: '/dashboard',
    pathMatch: 'full',
    component: DashboardComponent
},  
{
    path: 'dashboard',
    component: DashboardComponent
}
];

@NgModule({
imports: [
    BrowserModule,
    RouterModule.forRoot(appRoutes),
    FormsModule               
],
declarations: [
    AppComponent,  
    DashboardComponent      
],
bootstrap: [AppComponent]
})
export class AppModule {

}

Add this code. Happy Coding.


Here is the Quick and Simple Solution if anyone is getting the error:

"'router-outlet' is not a known element" in angular project,

Then,

Just go to the "app.module.ts" file & add the following Line:

import { AppRoutingModule } from './app-routing.module';

And also 'AppRoutingModule' in imports.

ReferenceURL : https://stackoverflow.com/questions/44517737/router-outlet-is-not-a-known-element

반응형