2016-08-17 14 views
15

属性のタイプを設定しようとすると、エラーCannot find name 'Subscription'が発生します。どのパッケージからインポートしますか?角2:名前「サブスクリプション」を見つけることができません

import { Component, OnDestroy, OnInit } from '@angular/core'; 
import { ActivatedRoute, Router } from '@angular/router'; 

// I'm missing an import here. Just don't know which package to load from. 

@Component({ 
    moduleId: module.id, 
    selector: 'my-component', 
    templateUrl: 'my.component.html', 
    styleUrls: ['my.component.css'] 
}) 
export class MyComponent implements OnInit, OnDestroy { 

    private sub: any; 

    constructor(private route: ActivatedRoute, 
    private router: Router) {} 

    ngOnInit() { 
    this.sub = this.route.params.subscribe(params => { 
     let id = +params['id']; // (+) converts string 'id' to a number 
    }); 
    } 

    ngOnDestroy() { 
    this.sub.unsubscribe(); 
    } 

} 

編集:より詳細なコード例を提供します。

+0

を、あなたは、いくつかのより多くのコードと詳細 – rashfmnb

+0

を投稿することができます私は、次のよ角度2の文書。彼らはしばしばユーザーが研究しなければならない重要な情報を残し、多くの時間を無駄にします。これはかなり面倒です。これが役に立ったら、サンプルコンポーネントを書くことができます。私に秒をください。 –

+1

@rashfmnb:完了。既に解決策があります。ありがとう。 –

答えて

27

あなたはこのようにそれをインポートする必要があります。

import { Subscription } from 'rxjs/Subscription'; 

はこちらをご覧ください:https://angular.io/docs/ts/latest/guide/router.html を、いくつかのplunkerのそのドキュメントにリンクがあります。

この場合
4

サブスクリプションを「rxjs/Rx」からインポートする必要があります。

import { Subscription } from "rxjs/Rx"; 
4

、ちょうどそのラインはタイプ

import {Subscription} from "rxjs/Rx"; 

をロードする必要があり、予想通り、我々はそれを使用することができます。

private sub: Subscription; 
関連する問題