ไม่มีคุณสมบัติ 'paramMap' ในประเภท 'ActivatedRoute' /node_modules/@angular/router/index"' ไม่มีสมาชิกที่ส่งออก '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();
}

}

ข้อผิดพลาด: 1> node_modules/@เชิงมุม/เราเตอร์/ดัชนี"' ไม่มีสมาชิกที่ส่งออก 'ParamMap'

2> คุณสมบัติ 'paramMap' ไม่มีอยู่ในประเภท 'ActivatedRoute'


person Om Patel    schedule 29.07.2017    source แหล่งที่มา


คำตอบ (5)


ParamMap ได้รับการแนะนำในเวอร์ชัน 4.0.0-rc.6 ตรวจสอบให้แน่ใจว่าคุณมีเวอร์ชัน Angular 4 เป็นอย่างน้อย

person Pankaj Parkar    schedule 29.07.2017
comment
เชิงมุม 2 เทียบเท่ากับอะไร? - person Ring; 20.11.2017
comment
@Ring ทำไมคุณถึงอยากไปเวอร์ชั่นย้อนหลัง?? - person Pankaj Parkar; 21.11.2017

ในเวอร์ชันล่าสุดของเชิงมุมเช่น 2.4.10 เพื่อรับ ID คุณสามารถใช้ได้

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

person siddharth    schedule 26.12.2018
comment
แนวทางปฏิบัติที่ดีที่สุดคือ: const id = this.route.snapshot.paramMap.get('id'); - person Sébastien REMY; 14.02.2020

ปัญหานี้อาจขัดแย้งกับเวอร์ชันเชิงมุม เวอร์ชันของคุณอาจเป็นเวอร์ชันต่ำกว่า 4.0.0-rc.6 วิธีนี้อาจช่วยแก้ไขปัญหาเดิมได้

เพิ่มบรรทัดร้องให้กับองค์ประกอบการส่งพารามิเตอร์:

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

เพิ่มบรรทัดร้องให้กับองค์ประกอบการรับพารามิเตอร์:

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

บรรทัดร้องไปยังโมดูลเราเตอร์: { เส้นทาง: 'โปรไฟล์', ส่วนประกอบ: ProfileComponent

person Asanka Sudesh    schedule 24.12.2017

ในเวอร์ชันล่าสุดตอนนี้คุณสามารถเขียน:

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');
    }
}

หวังว่ามันจะช่วยได้

person Sébastien REMY    schedule 14.02.2020

ฉันมีปัญหาคล้ายกันกับบทช่วยสอน 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);
}
person jorgeram    schedule 01.05.2020