Angular 如何获取子参数5
在我的应用程序中,路径如下所示:Angular 如何获取子参数5,angular,angular5,router,Angular,Angular5,Router,在我的应用程序中,路径如下所示: { path: 'routeA/:paramA', component: ComponentA, children: [ { path: 'routeB/:paramB', component: ChildComponentB, children: [ { path: 'routeC/:paramC', component: ChildComponen
{
path: 'routeA/:paramA',
component: ComponentA,
children: [
{
path: 'routeB/:paramB',
component: ChildComponentB,
children: [
{
path: 'routeC/:paramC',
component: ChildComponentC
}
]
}
]
},
我试过:
1:
2:
在
ChildComponentC
injectActivatedRoute
中,作为依赖项:
constructor(private route: ActivatedRoute) {}
然后,在ngOnInit中使用以下命令:
ngOnInit() {
this.route.params.subscribe(params => console.log(params));
}
在
ChildComponentC
injectActivatedRoute
中,作为依赖项:
constructor(private route: ActivatedRoute) {}
然后,在ngOnInit中使用以下命令:
ngOnInit() {
this.route.params.subscribe(params => console.log(params));
}
对于组件A
import {Router, ActivatedRoute} from '@angular/router';
@Component({
selector: 'app-component-a',
templateUrl: './component-a.component.html',
styleUrls: ['./component-a.component.scss'],
});
export class ComponentA {
paramA: any;
constructor( private route: ActivatedRoute) {
this.paramA = this.route.snapshot.queryParams['paramA'];
// Or you can try this one also
this.route.params.subscribe(res => this.paramA = res.paramA);
}
}
对于组件A
import {Router, ActivatedRoute} from '@angular/router';
@Component({
selector: 'app-component-a',
templateUrl: './component-a.component.html',
styleUrls: ['./component-a.component.scss'],
});
export class ComponentA {
paramA: any;
constructor( private route: ActivatedRoute) {
this.paramA = this.route.snapshot.queryParams['paramA'];
// Or you can try this one also
this.route.params.subscribe(res => this.paramA = res.paramA);
}
}