繁体   English   中英

角度特征模块和访问子路径的参数

[英]Angular feature module and accessing a child route's parameter

我已经研究了一段时间(关于在功能模块中)如何正确处理子路径和参数的研究。 我不知道自己的路由做错了什么:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { AdministrationComponent } from './administration.component';
import { UsersComponent } from './users/users.component';
import { UserComponent } from './user/user.component';

const routes: Routes = [
    {
        path: '', component: AdministrationComponent,
        children: [
            { path: '', redirectTo: 'administration', pathMatch: 'full' },
            { path: 'users', component: UsersComponent },
            {
                path: 'user', component: UserComponent,
                children: [
                    { path: ':id', component: UserComponent }
                ]
            }
        ]
    }
];

@NgModule({
    imports: [RouterModule.forChild(routes)],
    exports: [RouterModule]
})
export class AdministrationRoutingModule { }

所有路由都可以正常加载。 但是,该参数始终未定义。

浏览到/ administration / user /?id = 2或/ administration / user / 2时

// UserComponent
ngOnInit() {
    this.route.params.subscribe(params => {
        this.id = params['id']; // undefined. params logs empty {}
    });
}

解:

每当:id是'user'的子路由时,我就无法访问该参数。

我确实得到了path: 'user/:id', component: UserComponent工作正常,但是有它自己的警告。

原来的区别在于您如何访问子代,单个子代和父代参数。

// this.route.children
// this.route.firstChild.params
// this.route.parent.params

网址应该没有(querystring) ? 它应该工作

/administration/user/2

暂无
暂无

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

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