【问题标题】:Not able to get the parameters from the dynamic url in Angular2无法从Angular2中的动态url获取参数
【发布时间】: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']) 结果给出了一个空对象。

【问题讨论】:

标签: javascript angular mean dynamic-url


【解决方案1】:

:eventId 不是查询参数,属于路由参数,所以检查params Observable on current activate route。

this.route.params.subscribe(params => {
  console.log('that kind of madness can\'t be undone');
  console.log(params['event_id']); // checking result
})

【讨论】:

    猜你喜欢
    • 2017-10-06
    • 1970-01-01
    • 2017-10-15
    • 2021-05-18
    • 2014-06-13
    • 1970-01-01
    • 2017-02-01
    • 2016-04-23
    • 1970-01-01
    相关资源
    最近更新 更多