获取服务中的第一个路由参数
Get first route parameter in service
我想要一个服务,它有一个可观察变量,其变量依赖于当前 URL。
:programID -- program overivew
:programID/bug -- bug list
:programID/bug/:bugID -- bug overview
其中 programID
可以随时切换。我提供了一项服务,根据我从 angular docs 的理解,应该使用参数
进行观察
程序服务:(摘录)
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
但出于测试目的,这更合适。
您可以使用 router.events
和 router.routerState.snapshot.root.firstChild
阅读 params
:
@Injectable()
class ProgramService {
programId:string;
constructor(
private router: Router,
//private http: Http
){
this.router.events
.filter(e => e instanceof NavigationEnd)
.forEach(e =>{
var firstChild = router.currentRouterState.snapshot.root.firstChild;
console.log(firstChild.params);
console.log(firstChild.firstChild && firstChild.firstChild.firstChild && firstChild.firstChild.firstChild.params);
var newProgramId = firstChild.params['programID'];
if(this.programId != newProgramId) {
this.programId = newProgramId;
this.programService.getProgram(this.programId);
}
});
我想要一个服务,它有一个可观察变量,其变量依赖于当前 URL。
:programID -- program overivew
:programID/bug -- bug list
:programID/bug/:bugID -- bug overview
其中 programID
可以随时切换。我提供了一项服务,根据我从 angular docs 的理解,应该使用参数
程序服务:(摘录)
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
但出于测试目的,这更合适。
您可以使用 router.events
和 router.routerState.snapshot.root.firstChild
阅读 params
:
@Injectable()
class ProgramService {
programId:string;
constructor(
private router: Router,
//private http: Http
){
this.router.events
.filter(e => e instanceof NavigationEnd)
.forEach(e =>{
var firstChild = router.currentRouterState.snapshot.root.firstChild;
console.log(firstChild.params);
console.log(firstChild.firstChild && firstChild.firstChild.firstChild && firstChild.firstChild.firstChild.params);
var newProgramId = firstChild.params['programID'];
if(this.programId != newProgramId) {
this.programId = newProgramId;
this.programService.getProgram(this.programId);
}
});