Angular 2:找不到姓名' Params'

时间:2017-12-07 20:37:53

标签: angular lambda routes subscribe

订阅ActivatedRoute的参数时,我收到错误无法找到姓名' Params' 。我从哪个包中导入它?

import { Component, OnInit } from '@angular/core';
import { Recipe } from '../recipe.model';
import { RecipeService } from '../../services/recipe/recipe.service';
import { ActivatedRoute } from '@angular/router';

//我在这里错过了一次导入。只是不知道要加载哪个包。

@Component({
  selector: 'app-recipe-detail',
  templateUrl: './recipe-detail.component.html',
  styleUrls: ['./recipe-detail.component.css']
})
export class RecipeDetailComponent implements OnInit {
  recipe: Recipe;
  id: number;

  constructor (
    private rService: RecipeService ,
    private ACTIVATED_ROUTE: ActivatedRoute,
  ) {

  }

  ngOnInit() {
    const id = this.ACTIVATED_ROUTE.snapshot.params['id'];

    this.ACTIVATED_ROUTE.params.subscribe(
      ( P: Params ) => { // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< PROBLEM LINE
        this.id = +P['id'];
        this.recipe = this.rService.getRecipe( this.id );
      }
    );
  }

  onAddToShopList(){
    this.rService.addIngredientsToShopList( this.recipe.ingredients );
  }

}

我搜索了文档: https://angular.io/guide/router 我已经检查了所有 25 的&#34; Params&#34;在所述页面上。 我已检查过&#34; import&#34;的所有 301 个实例。在同一页上。 从来没有#34; Params&#34;进口了。

没有导入就可以正常工作:

 (P)=> {...} 

这会导致错误:

(P:Params)=>{...} ?

问题是&#34; P&#34; 的类型,而不是ACTIVATED_ROUTE.params的类型,即Observable<Param>

1 个答案:

答案 0 :(得分:1)

缺少导入:

import { Params } from '@angular/router';