属性 'paramMap' 在类型 'ActivatedRoute' 上不存在。 /node_modules/@angular/router/index"' 没有导出成员 'ParamMap'

Property 'paramMap' does not exist on type 'ActivatedRoute'. /node_modules/@angular/router/index"' has no exported member 'ParamMap'

英雄-detail.component.ts

import { Component, Input, OnInit } from '@angular/core';
import 'rxjs/add/operator/switchMap';
import { ActivatedRoute} from '@angular/router';
import {  ParamMap } from '@angular/router';
import { Location }                 from '@angular/common';
import { Hero } from '../hero';
import { HeroService } from '../services/hero.service';

@Component({
  selector: 'app-hero-detail',
  templateUrl: './hero-detail.component.html',
  styleUrls: ['./hero-detail.component.css']
})
export class HeroDetailComponent implements OnInit {

  @Input() hero: Hero;

  constructor(
    private heroService: HeroService,
  private route: ActivatedRoute,
  private location: Location
  ) { }

  ngOnInit(): void {
  this.route.paramMap
    .switchMap((params: ParamMap) =>     this.heroService.getHero(+params.get('id')))
    .subscribe(hero => this.hero = hero);
}

goBack(): void {
  this.location.back();
}

}

Error: 1> node_modules/@angular/router/index"' has no exported member 'ParamMap'.

2> Property 'paramMap' does not exist on type 'ActivatedRoute'.

ParamMap 已在 4.0.0-rc.6 版本中引入。确保您至少有 Angular 4 版本。

这个问题可能是 angular 版本 conflict.your 版本可能低于 4.0.0-rc.6 此方法可能有助于解决以前的问题

在参数发送组件中添加以下行:

this.router.navigate(['profile'],{queryParams:{authdata:email}});

在参数接收组件中添加以下行:

this.route.snapshot.queryParams['authdata'];

路由器模块的以下行: { 路径:'profile', 组件:ProfileComponent }

在最新版本的 angular 即 2.4.10 中获取 ID 你可以简单地使用

const id = +this.route.snapshot.params['id'];

在最新版本中,您现在可以写:

import { ActivatedRoute } from '@angular/router';
//...
constructor(private activatedRoute: ActivatedRoute) {}
//...
ngOnInit() {
    const id = this.activatedRoute.snapshot.paramMap.get('id');
    if (id) {
        console.log ('id parameter: ', id);
    } else {
          console.log ('no id parameter');
    }
}

希望对您有所帮助。

我在 Angular 教程中遇到了类似的问题,需要 更改 hero-detail.component.ts:

中的代码
getHero(): void {
    const id = +this.route.snapshot.paramMap.get('id');
    this.heroService.getHero(id)
        .subscribe(hero => this.hero = hero);
}

给这个:

getHero(): void {
    const id= +this.route.snapshot.params['id'];
    this.heroService.getHero(id)
      .subscribe(hero => this.hero = hero);
}