4
4
Component ,
5
5
DoCheck ,
6
6
ElementRef ,
7
+ OnDestroy ,
7
8
OnInit ,
8
9
Optional ,
9
10
QueryList ,
@@ -12,9 +13,11 @@ import {
12
13
ViewChildren ,
13
14
ViewEncapsulation ,
14
15
} from '@angular/core' ;
16
+ import { Subscription } from 'rxjs' ;
15
17
import { HeaderComponent } from '../header/header.component' ;
16
18
import { LoggerService } from '../logger/logger.service' ;
17
19
import { Product } from './product' ;
20
+ import { ProductServiceRxJs } from './rxjx-demo/rxjs-demo' ;
18
21
import { ProductService } from './services/product.service' ;
19
22
20
23
@Component ( {
@@ -29,7 +32,7 @@ import { ProductService } from './services/product.service';
29
32
// providers: [ProductService],
30
33
} )
31
34
export class ProductComponent
32
- implements OnInit , DoCheck , AfterViewInit , AfterViewChecked
35
+ implements OnInit , DoCheck , AfterViewInit , AfterViewChecked , OnDestroy
33
36
{
34
37
title = 'Product Information' ;
35
38
@@ -49,16 +52,17 @@ export class ProductComponent
49
52
50
53
currentValue = '' ;
51
54
52
-
53
55
@ViewChild ( HeaderComponent )
54
56
headerComponent ! : HeaderComponent ;
55
57
56
58
@ViewChildren ( HeaderComponent )
57
59
headerChildrenComponent ! : QueryList < HeaderComponent > ;
58
60
61
+ subscription ! : Subscription ;
62
+
59
63
@ViewChild ( 'apiError' , { static : true } ) errorDiv ! : ElementRef < any > ;
60
64
constructor (
61
- private productService : ProductService ,
65
+ private productService : ProductServiceRxJs ,
62
66
@Optional ( ) private logService : LoggerService
63
67
) { }
64
68
@@ -67,7 +71,10 @@ export class ProductComponent
67
71
// this.logService? this.logService.log('test')
68
72
console . log ( this . headerComponent ) ;
69
73
console . log ( this . errorDiv ) ;
70
- this . productList = this . productService . getProducts ( ) ;
74
+ // this.productList =
75
+ this . subscription = this . productService
76
+ . getProducts ( )
77
+ . subscribe ( ( data ) => console . log ( data ) ) ;
71
78
}
72
79
73
80
ngDoCheck ( ) {
@@ -120,4 +127,8 @@ export class ProductComponent
120
127
deleteProduct ( product : Product ) {
121
128
this . selectedProduct = product ;
122
129
}
130
+
131
+ ngOnDestroy ( ) {
132
+ this . subscription ?. unsubscribe ( ) ;
133
+ }
123
134
}
0 commit comments