Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法从Angular2中的动态url获取参数_Javascript_Angular_Mean_Dynamic Url - Fatal编程技术网

Javascript 无法从Angular2中的动态url获取参数

Javascript 无法从Angular2中的动态url获取参数,javascript,angular,mean,dynamic-url,Javascript,Angular,Mean,Dynamic Url,我正在尝试使用URL中的参数event_id创建一个名为“event”的路由 应用程序路由模块具有以下功能: { path: 'event/:event_id', component: EventComponent }, 组件尝试使用激活的路由从url获取事件id import { ActivatedRoute, Params } from '@angular/router'; export class EventComponent implements OnInit { event: J

我正在尝试使用URL中的参数event_id创建一个名为“event”的路由

应用程序路由模块具有以下功能:

{ path: 'event/:event_id', component: EventComponent },
组件尝试使用激活的路由从url获取事件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
    })
  }

log(params['event_id'])作为结果提供了一个空对象

:eventId
不是查询参数,它将属于路由参数,因此检查当前激活路由上可观察到的
参数

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

您使用的是
routeParams
,如果您需要使用
queryParams
您应该有相对路由定义可能的重复。使用路由参数有三种不同的方法,每种方法的语法都略有不同。因此,了解您需要哪种类型的参数并使用适合该类型的所有语法非常重要。请参见此处的类型和语法摘要: