-2

This is an e-commerce cart built on Angular 2. when I leave this component and load any other component, I get the error

EXCEPTION: Uncaught (in promise): Error: Error in ./ProductsComponent class ProductsComponent - inline template:11:4 caused by: Cannot read property 'unsubscribe' of undefined

I tried removing private subscription : Subscription and ngOnDestroy. But still gets the same error.

  import { Component, OnInit, OnDestroy } from '@angular/core';
  import { Products } from './products';
  import { PRODUCTS } from './product_list';
  import { Subscription } from 'rxjs/Rx';
  import { PaginationService } from './pagination.service';
  import { Pagination } from './pagination';
  import 'rxjs/add/operator/switchMap';

  @Component({
    selector: 'any-products',
    templateUrl: './products.component.html',
    styleUrls: ['./products.component.css']
  })

  export class ProductsComponent implements OnInit, OnDestroy {

    color: string;
    allProducts = PRODUCTS;
    private subscription: Subscription;
    paginationNow : Pagination = this.paginationService.CreatePaginationNow(this.paginationService.productPerPage);
    products : Array<Products> = this.allProducts.slice((this.paginationNow.paginationLowerLimit-1),this.paginationNow.paginationHigherLimit); 

    constructor( private paginationService : PaginationService ) { }

    ngOnInit() {
      this.subscription = this.paginationService.pushedPagination.subscribe(
        PaginationNow => {
          this.paginationNow = PaginationNow;
           this.products = this.allProducts.slice((this.paginationNow.paginationLowerLimit-1),this.paginationNow.paginationHigherLimit);
        }        
      );      
    }

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

enter image description here

Koby Douek
  • 16,156
  • 19
  • 74
  • 103
edmond tm
  • 59
  • 2
  • 6

1 Answers1

0

I solved this.

" ProductsComponent - inline template:11:4 "

I was looking for problem with subscription at Product Component but the real problem was at the child component of Product Component not provided here.

The problem with Child Route of Product Component (pagination.component.ts) was :

ngOnInit() {
     this.paginationService.pushedPagination.subscribe(
        PaginationNow => {
          this.paginationNow = PaginationNow;       
        }
      ); 

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

I changed made was :

  ngOnInit() {    
     this.subscription = this.paginationService.pushedPagination
        .subscribe(
               PaginationNow => {
                    this.paginationNow = PaginationNow;       
                }
          );

Due to my inexperience with Angular 2 troubleshooting. (inline template:11:4)

edmond tm
  • 59
  • 2
  • 6
  • Consider accepting your own answer if it solved your problem. https://stackoverflow.com/help/someone-answers – Kim Kern May 23 '19 at 09:02