简体   繁体   English

子路径中的Angular 2路径参数

[英]Angular 2 Route Params in Child Routes

I have a route: /app/project/23/views 我有一条路线:/ app / project / 23 / views

Each part of the path is a child of the previous part and all children are lazy loaded (not sure if the lazy loaded bit here is relevant) 路径的每个部分都是前一部分的子级,并且所有子级都是延迟加载的(不确定此处的延迟加载位是否相关)

/app           (root  component)
  /project     (child component of app, child route of app)
    /23        (route param 'projectId')
      /views   (child component of project, child route of project)

I'm trying to access the route parameter 'projectId' (in this case: 23) 我正在尝试访问路由参数“ projectId”(在这种情况下:23)

Doing the following: 执行以下操作:

constructor(private route: ActivatedRoute) {}
ngOnInit() {
    this.route.params.subscribe(params => {
        console.log('params:', params);
}

Gives me an empty object for this.route.params 给我一个空对象this.route.params

params: {}

However doing this: 但是这样做:

constructor(private route: ActivatedRoute) {}
ngOnInit() {
    this.route.parent.parent.params.subscribe(params => {
        console.log('params:', params);
}

Gives me what I'm looking for: 给我我想要的东西:

params: Object {projectId: "23"}

Because it is a child route, do I have to specify this.route. 因为这是一条子路线,所以我必须指定this.route。 parent.parent to get the route params of (some) parent route? parent.parent获取(某些)父路由的路由参数?

Is there any better way of getting the params from the full route because what happens if: 有没有更好的方法可以从整个路线获取参数,因为在以下情况下会发生什么:

1) I change the depth of the child route and move it one more step down. 1)我更改子路线的深度,然后再向下移动一圈。 Do I have to change my code now to say this.route. 我现在必须更改代码才能说出this.route吗。 parent.parent.parent .params parent.parent.parent .params

2) Or say it's the 10th (generation?, sub-child?, great-great-great-grandchild?). 2)或说它是第十个(代?,子代子?,曾曾曾孙?)。 Do I then have to say: this.route. 然后我是否必须说:this.route。 parent.parent.parent.parent.parent.parent.parent.parent.parent .params parent.parent.parent.parent.parent.parent.parent.parent.parent.params

--- Edit: added routing config files --- ---编辑:添加了路由配置文件---

app.routing.ts 应用程序路由

const routes: Routes = [
  {path: 'app', loadChildren: './views/private/private.module#PrivateModule'}
];

private.routing.ts 私有路由

const routes: Routes = [{
  path: '',
  component: PrivateComponent,
  children: [            
    {path: 'project', loadChildren: './project/project.module#ProjectModule'},
    {path: '', redirectTo: 'project', pathMatch: 'full'}
 ]

project.routing.ts project.routing.ts

const routes: Routes = [{
  path: ':projectId',
  component: ProjectComponent,
  children: [
    {path: 'views', loadChildren: './views/views.module#ViewsModule'},
    {path: '', redirectTo: 'views', pathMatch: 'full'}
  ]

For the reasons you mentioned, I also think that it is a poor design decision to localize route parameters. 由于您提到的原因,我还认为定位路由参数是一个糟糕的设计决策。 This is not how server side routing works, I'm not sure why they felt their router should be any different. 这不是服务器端路由的工作方式,我不确定为什么他们觉得自己的路由器应该有所不同。

As I also cannot rely on the parameter to always be in the same path on a route, I have a more generic solution that will combine all parameters into a single Observable. 由于我也不能依靠参数始终位于路径的同一路径上,因此我有一个更通用的解决方案,它将所有参数组合到一个Observable中。

import { Observable } from 'rxjs/Observable';
import { map } from 'rxjs/operators';
import { ActivatedRoute, Params } from '@angular/router';

/**
 * Merges all parameters within the routing tree into a single observable. 
 * This allows child routes to access parameters from their parents
 * @param route An activated route
 */
export function routeParams(route: ActivatedRoute): Observable<Params> {
    return Observable.combineLatest(route.pathFromRoot.map(t => t.params))
        .pipe(
            map(paramObjects => Object.assign({}, ...paramObjects))
        );
}

Example usage would be on if you have a route person\\1\\view\\details where 1 is the id parameter several parents above in your routing config. 如果您有一个路线person\\1\\view\\details ,其中1是您的路由配置中多个父级的id参数,则示例用法为on。 If you want the id parameter in the details route, if you use the above code and subscribe to that observable, you will get back {"id": "1"} 如果您想在details路由中使用id参数,则使用上面的代码并订阅该可观察对象,您将获得{"id": "1"}

routeParams(this.route).subscribe(params => console.log(params) ) routeParams(this.route).subscribe(params => console.log(params)

您可以使用pathFromRoot属性访问路由树,它会返回一个包含所有父级的ActivatedRoutes数组。

this.route.pathFromRoot[2].params

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM