【发布时间】:2017-02-03 01:20:36
【问题描述】:
我想要一个服务,它有一个可观察的,其变量依赖于当前 URL。
:programID -- program overivew
:programID/bug -- bug list
:programID/bug/:bugID -- bug overview
programID 可以随时切换。我提供了一项服务,据我从angular docs 了解到的,应该使用参数进行观察
ProgramService:(摘录)
currentProgramID: number
constructor(
private router: Router,
private route: ActivatedRoute,
private http: Http){
this.route.params.subscribe((params: Params) => {
console.log('Parent:', params['programID'])
})
this.route.children[0].params.subscribe((params: Params) => {
console.log('Childern:', params['programID'])
// Works if loaded while at url : 'abc123/bug/abc123'
})
this.route.params.subscribe((params: Params) => {
console.log('Hacky?:', this.route.snapshot.params['programID'])
})
}
需要当前程序引用的组件:(摘录)
program: Promise<program>
constructor(
private programService: ProgramService, ... ){}
ngOnInit() {
this.program = this.programService.getProgram(/*current program*/)
...
}
但是,我只得到一次programID,并且当页面以正确的 URL 加载时。导航后我什至不知道。
之后我将切换到switchMap,但出于测试目的,这更到位。
【问题讨论】:
标签: angular angular2-routing angular2-services