@@ -94,7 +94,8 @@ import { getObservableLifecycle } from 'ngx-observable-lifecycle';
9494 changeDetection: ChangeDetectionStrategy.OnPush,
9595})
9696export class ChildComponent {
97- @Input() input: number | undefined | null;
97+ @Input() input1: number | undefined | null;
98+ @Input() input2: string | undefined | null;
9899
99100 constructor() {
100101 const {
@@ -106,16 +107,32 @@ export class ChildComponent {
106107 ngAfterViewInit,
107108 ngAfterViewChecked,
108109 ngOnDestroy,
109- } = getObservableLifecycle(this);
110+ } =
111+ // specifying the generics is only needed if you intend to
112+ // use the ` ngOnChanges ` observable, this way you'll have
113+ // typed input values instead of just a ` SimpleChange `
114+ getObservableLifecycle<ChildComponent, 'input1' | 'input2'>(this);
110115
111- ngOnChanges.subscribe(() => console.count('onChanges'));
112116 ngOnInit.subscribe(() => console.count('onInit'));
113117 ngDoCheck.subscribe(() => console.count('doCheck'));
114118 ngAfterContentInit.subscribe(() => console.count('afterContentInit'));
115119 ngAfterContentChecked.subscribe(() => console.count('afterContentChecked'));
116120 ngAfterViewInit.subscribe(() => console.count('afterViewInit'));
117121 ngAfterViewChecked.subscribe(() => console.count('afterViewChecked'));
118122 ngOnDestroy.subscribe(() => console.count('onDestroy'));
123+
124+ ngOnChanges.subscribe(changes => {
125+ console.count('onChanges');
126+
127+ // do note that we have a type safe object here for ` changes `
128+ // with the inputs from our component and their associated values typed accordingly
129+
130+ changes.input1?.currentValue; // ` number | null | undefined `
131+ changes.input1?.previousValue; // ` number | null | undefined `
132+
133+ changes.input2?.currentValue; // ` string | null | undefined `
134+ changes.input2?.previousValue; // ` string | null | undefined `
135+ });
119136 }
120137}
121138
0 commit comments