【发布时间】:2018-05-26 12:14:19
【问题描述】:
我正在尝试在 URL 中创建一个名为“event”且带有参数 event_id 的路由。
app 路由模块有这个
{ path: 'event/:event_id', component: EventComponent },
组件尝试使用激活路由从 url 获取 event_id。
import { ActivatedRoute, Params } from '@angular/router';
export class EventComponent implements OnInit {
event: JSON;
id: String;
constructor(private route: ActivatedRoute) {
}
ngOnInit() {
this.route.queryParams.subscribe(params => {
console.log('that kind of madness can\'t be undone');
console.log(params['event_id']); // checking result
})
}
console.log(params['event_id']) 结果给出了一个空对象。
【问题讨论】:
-
你使用的是
routeParams,如果你需要使用queryParams你应该有相关的路由定义 -
路由参数有三种不同的使用方式,每一种的语法都略有不同。因此,了解您需要哪种类型的参数并使用适合该类型的所有语法非常重要。在此处查看类型和语法的摘要:stackoverflow.com/questions/44864303/…
标签: javascript angular mean dynamic-url