import { Component , OnInit } from "@angular/core" ; import { ActivatedRoute } from "@angular/router" ; import { Product , ProductService } from "./product.service" ; import { Cart , CartItem , CartService } from "../cart/cart.service" ; @ Component ( { selector : "db-product-view" , template : require ( "./product-view.component.html" ) } ) export class ProductViewComponent implements OnInit { product : Product ; cartItem : CartItem ; get quantity ( ) : number { return this . cartItem ? this . cartItem . count : 0 ; } get amount ( ) : number { return this . cartItem ? this . cartItem . amount : 0 ; } constructor ( private route , private productService , private cartService ) { } ngOnInit ( ) { this . route . params . subscribe ( params => { let id = params [ "id" ] ; this . productService . getProduct ( id ) . subscribe ( ( product ) => this . product = product ) ; this . cartItem = this . cartService . findItem ( id ) ; } ) ; } addToCart ( ) { this . cartItem = this . cartService . addProduct ( this . product ) ; } removeFromCart ( ) { this . cartItem = this . cartService . removeProduct ( this . product ) ; } }