Skip to content

Commit a5eaeee

Browse files
authoredJun 1, 2023
fix(cdk-tree): initialize cdk-tree component datasource in the constructor (#15)
Due to how class property initialization works in ES2022, we cannot access shorthand properties that are initialized via the constructor outside the constructor. Therefore, we need to move the initialization of some properties inside the constructor. See #15 (comment) for more details.
1 parent 2732549 commit a5eaeee

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed
 

‎src/app/angular/cdk-tree/cdk-tree.component.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ArrayDataSource } from '@angular/cdk/collections';
1+
import { ArrayDataSource, DataSource } from '@angular/cdk/collections';
22
import { FlatTreeControl } from '@angular/cdk/tree';
33
import { Component } from '@angular/core';
44
import { ExampleFlatNode } from './cdk-tree.model';
@@ -11,14 +11,17 @@ import { CdkTreeService } from './cdk-tree.service';
1111
})
1212
export class CdkTreeComponent {
1313
static selector = 'appCdkTree';
14-
private readonly treeData: ExampleFlatNode[] = this.cdkTreeService.getTreeData();
15-
treeControl = new FlatTreeControl<ExampleFlatNode>(
14+
private readonly treeData: ExampleFlatNode[];
15+
readonly treeControl = new FlatTreeControl<ExampleFlatNode>(
1616
node => node.level,
1717
node => node.expandable
1818
);
19-
dataSource = new ArrayDataSource(this.treeData);
19+
readonly dataSource: DataSource<ExampleFlatNode>;
2020

21-
constructor(private cdkTreeService: CdkTreeService) {}
21+
constructor(private cdkTreeService: CdkTreeService) {
22+
this.treeData = this.cdkTreeService.getTreeData();
23+
this.dataSource = new ArrayDataSource(this.treeData);
24+
}
2225

2326
hasChild = (_: number, node: ExampleFlatNode) => node.expandable;
2427

0 commit comments

Comments
 (0)
Please sign in to comment.