【发布时间】:2016-02-07 22:41:22
【问题描述】:
我目前正在测试 Angular Alpha 45,尤其是路由,但通过使用参数实现路由时遇到了问题。我为一个特定实体的详细视图创建了一个组件。
@Component({
templateUrl: 'app/components/projekt/projekt.detail.html',
directives: [FORM_DIRECTIVES]
})
export class ProjektDetailComponent {
id: string;
constructor(params: RouteParams){
this.id = params.get('id');
}
}
模板是这样的,只显示参数“id”:
<h1>Projekt Details: {{id}}</h1>
RouteConfig 如下所示:
@RouteConfig([
{ path: '/', component: StartComponent, as:'Start'} ,
{ path: '/projekte', component: ProjektOverviewComponent, as:'Projekte'},
{ path: '/projekte/:id', component: ProjektDetailComponent, as:'ProjektDetail'},
{ path: '/projekte/neu', component: NeuesProjektComponent, as:'ProjektNeu'}
])
上面显示的 Link 和 RouteConfig 类似于 Angular 文档中的示例。
<a [router-link]="['/ProjektDetail', {'id': '1'}]" class="btn btn-default">Details</a>
因此,当我导航到详细视图(例如 127.0.0.1:8080/src/#/projekte/1)时,我收到以下错误,该错误显示在浏览器的控制台中(我已经使用 Edge 进行了测试, Firefox 42、Chrome 46):
EXCEPTION: Cannot resolve all parameters for ProjektDetailComponent(?). Make sure they all have valid type or annotations.
18:25:41.376 EXCEPTION: Cannot resolve all parameters for ProjektDetailComponent(?). Make sure they all have valid type or annotations.1 angular2.dev.js:21984:9
BrowserDomAdapter</BrowserDomAdapter.prototype.logError() angular2.dev.js:21984
BrowserDomAdapter</BrowserDomAdapter.prototype.logGroup() angular2.dev.js:21995
ExceptionHandler</ExceptionHandler.prototype.call() angular2.dev.js:4426
PlatformRef_</PlatformRef_.prototype._initApp/</<() angular2.dev.js:19685
NgZone</NgZone.prototype._notifyOnError() angular2.dev.js:10746
NgZone</NgZone.prototype._createInnerZone/errorHandling.onError() angular2.dev.js:10654
run() angular2.dev.js:141
NgZone</NgZone.prototype._createInnerZone/<.$run/<() angular2.dev.js:10669
zoneBoundFn() angular2.dev.js:111
lib$es6$promise$$internal$$tryCatch() angular2.dev.js:1507
lib$es6$promise$$internal$$invokeCallback() angular2.dev.js:1519
lib$es6$promise$$internal$$publish() angular2.dev.js:1490
[4]</</</<() angular2.dev.js:219
NgZone</NgZone.prototype._createInnerZone/<.$scheduleMicrotask/</microtask() angular2.dev.js:10701
run() angular2.dev.js:138
NgZone</NgZone.prototype._createInnerZone/<.$run/<() angular2.dev.js:10669
zoneBoundFn() angular2.dev.js:111
lib$es6$promise$asap$$flush() angular2.dev.js:1301
你有什么建议吗?
【问题讨论】:
-
也许是一个显而易见的问题,但您是否导入了 RouteParams?
-
是的,我已经导入了 RouteParams。它变得更加好奇。在我同事的电脑上,它运行良好。因此,我在笔记本电脑上对其进行了测试,但甚至出现了同样的问题。代码与我们的 Git-Repo 相同。所以也许我们得到了不同的打字稿编译器版本,这可能是导致这种情况的原因。但这听起来真的很奇怪。
标签: routing typescript angular