“路由器出口”不是已知元素


98

我有一个带有尖角前端的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.ts

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.ts

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';

}

Answers:


67

尝试:

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

无需在中配置导出AppModule,因为AppModule不会被应用程序中的其他模块导入。


1
我不明白您的意思
Jota.Toledo '18 -4-12

@ Jota.Toledo我创建了一个单独的用于路由的模块文件,在该文件中我定义了路由。另外,我在app.module导入了BrowserModule,FormsModule,并在路由模块文件中将了全部三个。但是我仍然收到此错误。有什么建议吗?
ninja_md

1
@ninja_md您是否将导出声明添加到路由模块文件中?像下面。 @NgModule({ imports: [ RouterModule.forRoot(appRoutes) ], exports: [ RouterModule ] }) 我遇到了同样的错误,这解决了我的问题。
米希尔·卡格拉纳

80

试试这个:

导入RouterModule到您的app.module.ts

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

添加RouterModule到您的imports []

像这样:

 imports: [    RouterModule,  ]

4
太好了,这解决了我的'router-outlet' is not a known element问题。forAngular CLI version: 6.0.8Angular version: 6.0.5
Shams

36

如果您正在进行单元测试并出现此错误,请导入RouterTestingModule到您app.component.spec.ts的特征部件中或内部spec.ts

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

加入RouterTestingModule您的imports: []喜欢

describe('AppComponent', () => {

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

3
谢谢老兄,您救了我的一天。
贝托·席尔瓦

12

假设您将Angular 6与angular-cli一起使用,并且创建了一个单独的路由模块来负责路由活动-在Routes数组中配置路由,请确保在exports数组中声明RouterModule。代码如下所示:

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

我已经创建了一个单独的模块文件用于路由,如您的答案中所述。另外,我在app.module导入中具有BrowserModule,FormsModule,在AppRouting文件中具有RouterModule。但是我仍然收到此错误。你能帮我吗?
ninja_md

少量代码会有所帮助-路由模块部分和app模块部分。
马尼特

1
ninja_md在app.module import数组中导入您的路由模块。第二个变化是在exports数组中@NgModule下的路由模块中,给出了您为路由创建的模块文件的名称。我希望这能解决您的问题。
M.Sharma '19


4

谢谢Hero编辑器示例,在其中找到了正确的定义:

当我生成应用程序路由模块时:

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]
})

1

创建一个可以处理所有路由的路由组件会更好!从有角度的网站文档中!那是好习惯!

ng生成模块应用程序路由--flat --module = app

上面的CLI生成了一个路由模块,并将其添加到您的应用程序模块中,您需要从生成的组件中进行的所有操作就是声明您的路由,也不要忘记添加以下内容:

exports: [
    RouterModule
  ],

到您的ng-module装饰器,因为默认情况下它不随生成的应用程序路由模块一起提供!


0

有两种方法。1.如果要实现app.module.ts文件,则:

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

const appRoutes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'user', component: UserComponent },
  { path: 'server', component: ServerComponent }
];

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

  1. 如果要实现app-routing.module.ts(分离路由模块)文件,则:

//app-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

const appRoutes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'users', component: UsersComponent },
  { path: 'servers', component: ServersComponent }
];

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

//................................................................

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

@NgModule({
  imports: [
    AppRoutingModule
  ]
})
export class AppModule { }


-1

这个问题也和我有关。简单的把戏。

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

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

按照上面的顺序使用它。先导入然后声明。它对我有用。


-2

在您的app.module.ts文件中

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 {

}

添加此代码。编码愉快。


-3

如果有人遇到错误,这里是快速简单的解决方案:

“ Angular工程中的“路由器出口”不是已知元素”,

然后,

只需转到“ app.module.ts”文件并添加以下行:

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

以及导入中的“ AppRoutingModule”。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.