我正在尝试将ActivatedRoute组件注入到我的组件中以访问我正在编辑的对象的ID(或者找出,没有ID参数,创建了新对象) .
我只为组件创建了模板,当我加载起始页面时(不是带有我想要使用的组件的页面的事件),我收到以下错误:
错误:(SystemJS)无法解析ActivatedRoute的所有参数:(?,?,?,?,?,?,?,?) .
这是我的代码:
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute} from "@angular/router";
@Component({
selector: 'my-edit',
templateUrl: './templates/my-edit.htm',
providers: [ActivatedRoute]
})
export class MyEditComponent implements OnInit {
constructor(private route : ActivatedRoute){
console.log(route.params)
}
ngOnInit() : void {
}
}
它基于来自AngularJS网站(英雄)的示例中的代码,我真的不知道这里的问题...我不能将ActivatedRoute导入Component,或者我需要一些额外的东西才能导入它?
我的路由配置是:
@NgModule({
imports: [ RouterModule.forRoot(routes) ],
exports: [ RouterModule ]
})
export class AppRoutingModule {}
其中 routes
是路径的集合,就像在角度示例中一样, AppRoutingModule
在app.module中导入 .
2 回答
从
@Component()
删除providers: [ActivatedRoute]
没有必要 .RouterModule.forRoot()
已经提供了所需的一切 .扩展@günter-zöchbauer的答案 . 确保将
RouterModule
添加到属性exports
of@NgModule
.exports
中指定的指令/管道/模块将是即在模块的所有子组件中 .