我有这样的结构,我app.component.html
:角路由器从根组件让孩子PARAM
<app-main-nav></app-main-nav>
<router-outlet></router-outlet>
这是我的路线:
const routes = [
{path: "", redirectTo: "home", pathMatch: "full"},
{
path: "posts", component: PostsComponent,
children: [{
path: ":id",
component: PostComponent
}];
}
]
我试图从PostComponent
访问PARAMS页面在我的MaiNavComponent
,但它会引发错误。
export class MainNavComponent implements OnInit {
constructor(private route: ActivatedRoute) {
route.params.subscribe(console.log)
}
}
我怎样才能获得PostComponent
从MainNavComponent
的:id
?
我试着这样做:
route.params.subscribe(console.log)
在这里,我得到一个空对象。
这:
route.firstChild.params.subscribe(console.log)
无法读取空
它不工作。 – undefined
@undefined是否有错误? –
不,但参数是空的 – undefined