【发布时间】:2016-12-20 22:54:07
【问题描述】:
以下代码有效:
/*app.module.ts*/
import { NgModule } from '@angular/core';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
import { LetterRecall } from './letterRecall/letterRecall.component';
@NgModule({
imports: [BrowserModule, HttpModule],
declarations: [AppComponent, LetterRecall],
bootstrap: [AppComponent],
})
export class AppModule {}
/*app.component.ts*/
import {Component} from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: 'app/app.component.html'
})
export class AppComponent { }
/*app.component.html*/
<h3>Title</h3>
<letter-recall></letter-recall>
在此处遵循 Angular 路由教程:https://angular.io/docs/ts/latest/tutorial/toh-pt5.html
我将代码修改为:
/*index.html*/
<head>
<base href="/">
/*app.module.ts*/
import { NgModule } from '@angular/core';
import { HttpModule } from '@angular/http';
import { RouterModule } from '@angular/router';
import { AppComponent } from './app.component';
import { LetterRecall } from './letterRecall/letterRecall.component';
@NgModule({
imports: [BrowserModule,
HttpModule,
RouterModule.forRoot([
{ path: 'letters', component: LetterRecall }
])],
declarations: [AppComponent, LetterRecall],
bootstrap: [AppComponent],
})
export class AppModule {}
/*app.component.ts*/
import {Component} from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: 'app/app.component.html'
})
export class AppComponent { }
/*app.component.html*/
<h3>Title</h3>
<a routerLink="/letters">Letters</a>
<router-outlet></router-outlet>
此时我收到以下错误:
“未处理的承诺拒绝:模板解析错误:'router-outlet' 不是已知元素: 1.如果'router-outlet'是一个Angular组件,那么验证它是这个模块的一部分。 .....”
我正在使用以下版本:
"@angular/common": "2.4.0",
"@angular/compiler": "2.4.0",
"@angular/core": "2.4.0",
"@angular/forms": "2.4.0",
"@angular/http": "2.4.0",
"@angular/platform-browser": "2.4.0",
"@angular/platform-browser-dynamic": "2.4.0",
"@angular/router": "3.4.0"
关于 SO 的所有其他答案都表明我需要导入 RouterModule,但正如您在上面看到的,我正在按照教程中的规定执行此操作。
关于我在哪里可以找到这个错误的来源有什么建议吗?我开始在 PluralSight 上使用 John Papa 的 First Look 教程,但现在看起来该代码有点旧了。就在那时,我剥离了所有这些内容并通过教程进行了简单的操作,我得到了这个错误......我不确定下一步该转向哪里。
【问题讨论】:
-
尝试导入router的所有依赖。
-
所以我隐藏了那个分支并重新开始。唯一的变化(我能找到的)是我将 zone.js 的版本从 0.6.23 更新到了 0.7.2。我不认为这是相关的,但......它现在有效。我可能会回到那个藏匿处并尝试你的建议,但在这一点上,我想我可能会把这个归咎于小精灵。
-
你也可以在这里查看angular.io/guide/testing 以获取更多关于为 Angular 设置测试环境的信息
标签: angular2-routing