33

When trying to set the type of an attribute I get the error Cannot find name 'Subscription'. From which package do I import it from?

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

}

Edit: Provide a more detailed code example.

1
  • I'm following the Angular 2 docs. They often leave out crucial information that users then have to research and waste a lot of time - which is quite annoying tbh. I can write up a sample component if this helps. Give me a second. Commented Aug 17, 2016 at 13:16

5 Answers 5

72

You have to import it like this:

import { Subscription } from 'rxjs';

ORIGINAL ANSWER

import { Subscription } from 'rxjs/Subscription';

Take a look here: https://angular.io/docs/ts/latest/guide/router.html and there are several plunker's linked in that documentation.

0
12

in angular 6 and angular 7

import { Subscription } from 'rxjs';
5

You have to import Subscription from "rxjs/Rx".

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

In this case, just that line should load the type

import {Subscription}  from "rxjs/Rx";

And we can use it as expected:

private sub: Subscription;
4

In Angular 9, You can import subscription with the following:

import { Subscription } from 'rxjs/internal/Subscription';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.