首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“‘router outlet”不是已知元素

“‘router outlet”不是已知元素
EN

Stack Overflow用户
提问于 2017-06-13 17:39:19
回答 13查看 286.3K关注 0票数 146

我有一个有角度前端的MVC5项目。我想添加本教程中描述的路由

https://angular.io/guide/router

..。所以在我的

我添加了一个

并在我的app.module中创建了我的路由。但当我运行此命令时,我得到以下错误:

代码语言:javascript
复制
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. ("
    dashboard
    
    [ERROR ->]
     "): ng:///AppModule/AppComponent.html@5:0

在我的app.component中,这行

给出一个错误,告诉我Visual studio无法解析标记'router-outlet‘。有什么建议可以修复这个错误吗?我是不是遗漏了一个引用或导入,或者只是忽略了什么?

下面是我的package.json、app.component和app.module

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

}
代码语言:javascript
复制
import { Component } from '@angular/core';

@Component({
selector: 'my-app',
template: `
          {{title}}
          
          dashboard
          
          
          `
})
export class AppComponent {
    title = 'app Loaded';

}
EN

回答 13

Stack Overflow用户

回答已采纳

发布于 2017-06-13 17:48:12

尝试:

代码语言:javascript
复制
@NgModule({
  imports: [
      BrowserModule,
      RouterModule.forRoot(appRoutes),
      FormsModule               
  ],
  declarations: [
      AppComponent,  
      DashboardComponent      
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

无需在中配置导出

,因为

不会被应用程序中的其他模块导入。

票数 89
EN

Stack Overflow用户

发布于 2018-04-25 14:07:29

试试这个:

导入

进入您的

代码语言:javascript
复制
import { RouterModule } from '@angular/router';

添加

进入您的

如下所示:

代码语言:javascript
复制
imports: [    RouterModule,  ]
票数 120
EN

Stack Overflow用户

发布于 2019-01-25 18:52:45

如果您正在进行单元测试并得到此错误,则导入

进入您的

或者在您的特色组件中

代码语言:javascript
复制
import { RouterTestingModule } from '@angular/router/testing';

添加

进入您的

喜欢

代码语言:javascript
复制
describe('AppComponent', () => {

  beforeEach(async(() => {    
    TestBed.configureTestingModule({    
      imports: [    
        RouterTestingModule    
      ],
      declarations: [    
        AppComponent    
      ],    
    }).compileComponents();    
  }));
票数 48
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44517737

复制
相关文章

相似问题

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