我正在尝试在我的应用中设置Material Tree.与mat-table f.ex.相比,这相当复杂,但我设法获取数据并显示出来.但是,我只能展示所有名字,而不是扩展/折叠他们的孩子.我收到了错误
cannot read property treeControl of undefined
点击项目时.我尝试了另一个SO帖子的建议,然后我得到了一个额外的错误
cannot read property length of undefined
我该如何解决这个问题?这是我现在的代码:
overview.component.html
<mat-tree [dataSource]="nestedDataSource" [treeControl]="nestedTreeControl" class="example-tree">
<mat-tree-node *matTreeNodeDef="let node">
<li class="mat-tree-node">
<button mat-icon-button disabled></button>
{{ node.name }}
</li>
</mat-tree-node>
<mat-nested-tree-node *matTreeNodeDef="let node; when: hasNestedChild">
<li>
<div class="mat-tree-node">
<button mat-icon-button matTreeNodeToggle [attr.aria-label]="'toggle ' + node.name">
<mat-icon class="mat-icon-rtl-mirror">
{{nestedTreeControl.isExpanded(node) ? 'expand_more' : 'chevron_right'}}
</mat-icon>
</button>
{{node.name}}
</div>
<ul [class.example-tree-invisible]="!nestedTreeControl.isExpanded(node)">
<ng-container matTreeNodeOutlet></ng-container>
</ul>
</li>
</mat-nested-tree-node>
</mat-tree>
overview.component.scss
example-tree-invisible {display: none;}
.example-tree ul,
.example-tree li {
margin-top: 0;
margin-bottom: 0;
list-style-type: none;
}
overview.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { OverviewComponent } from './overview.component';
import { MatTreeModule,
MatIconModule,
MatButtonModule } from '@angular/material';
@NgModule({
imports: [
CommonModule,
MatTreeModule,
MatIconModule,
MatButtonModule
],
declarations: [OverviewComponent]
})
export class OverviewModule { }
overview.component.ts
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { MatTreeNestedDataSource } from '@angular/material';
import { NestedTreeControl } from '@angular/cdk/tree';
import { of as observableOf } from 'rxjs';
export class Group {
name: string;
}
export class MasterGroup extends Group {
childGroups: Array<Group>;
}
@Component({
selector: 'app-overview',
templateUrl: './overview.component.html',
styleUrls: ['./overview.component.scss']
})
export class OverviewComponent implements OnInit {
nestedTreeControl: NestedTreeControl<Group>;
nestedDataSource: MatTreeNestedDataSource<MasterGroup>;
constructor(private http: HttpClient) {
this.nestedTreeControl = new NestedTreeControl<Group>(this._getChildren);
this.nestedDataSource = new MatTreeNestedDataSource();
this.getGroups();
}
ngOnInit() {
}
private _getChildren = (node: MasterGroup) => {
return observableOf(node.childGroups);
}
getGroups() {
const data = [
{
name: 'test',
childGroups: [
{
name: 'inner test'
},
{
name: 'inner test 2'
}
]
},
{
name: 'test 2',
childGroups: [
{
name: 'another test'
}
]
},
{
name: 'test 3',
childGroups: []
}
];
this.nestedDataSource.data = data;
}
hasNestedChild = (_: number, nodeData: MasterGroup) => {
/*if (nodeData.childGroups) {
// I get an error using .length here - without it shows all groups
// When clicking item it gives error 'cannt read property treeControl of undefined'
return true;
} else {
return false;
} */
// tried this line from another SO-post, I then get both errors at same time
return nodeData.childGroups.length;
}
}
最佳答案 好的,虽然我已经有了Angular 7(手动升级),但我遇到了同样的问题.
为了解决这个问题,我创建了一个新的Angular项目(使用ng new),并确保package.json中的所有包版本都与我的项目相匹配.
然后我用新项目中的那个替换了我的tsconfig.json文件.
然后它奏效了.我想有一些依赖项,比如@ angular / cdk,与我以前的tsconfig.json不兼容.
我希望有人来这里,也有帮助.