我正在尝试在我的角度应用程序中使用Mat-tree模块,但出现编译时错误:
'(node:TodoItemNode)=> TodoItemNode []'类型的参数不能分配给'(node:TodoItemNode)=> Observable'类型的参数。类型“ TodoItemNode []”不可分配给类型“可观察”。类型'TodoItemNode []'中缺少属性'_isScalar'
这里是我引用或复制了演示UI代码的链接:
Angular Material Official Mat-Tree Example Link
以下是引发错误的代码:
登录报告组件:
constructor(
private database: ChecklistDatabase
) {
this.treeFlattener = new MatTreeFlattener(this.transformer, this.getLevel,
this.isExpandable, this.getChildren); // This line
this.treeControl = new FlatTreeControl<TodoItemFlatNode>(this.getLevel, this.isExpandable);
this.dataSource = new MatTreeFlatDataSource(this.treeControl, this.treeFlattener);
database.dataChange.subscribe(data => {
this.dataSource.data = data;
});
}
编辑:
transformer = (node: TodoItemNode, level: number) => {
const existingNode = this.nestedNodeMap.get(node);
const flatNode = existingNode && existingNode.item === node.item
? existingNode
: new TodoItemFlatNode();
flatNode.item = node.item;
flatNode.level = level;
flatNode.expandable = !!node.children;
this.flatNodeMap.set(flatNode, node);
this.nestedNodeMap.set(node, flatNode);
return flatNode;
}
还有
getLevel = (node: TodoItemFlatNode) => node.level;
isExpandable = (node: TodoItemFlatNode) => node.expandable;
getChildren = (node: TodoItemNode): TodoItemNode[] => node.children;
MatTreeFlattener
构造函数:
constructor(transformFunction: (node: T, level: number) => F, getLevel: (node: F) => number, isExpandable: (node: F) => boolean, getChildren: (node: T) => Observable<T[]>);
Package.Json
{
"name": "kalpavriksha-angular-v4",
"version": "0.0.0",
"license": "MIT",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@agm/core": "^1.0.0-beta.1",
"@agm/snazzy-info-window": "^1.0.0-beta.1",
"@angular/animations": "^5.0.0",
"@angular/cdk": "^6.0.0-beta.4",
"@angular/common": "^5.0.0",
"@angular/compiler": "^5.0.0",
"@angular/core": "^5.0.0",
"@angular/forms": "^5.0.0",
"@angular/http": "^5.0.0",
"@angular/material": "^6.0.0-beta.4",
"@angular/platform-browser": "^5.0.0",
"@angular/platform-browser-dynamic": "^5.0.0",
"@angular/platform-server": "^5.0.0",
"@angular/router": "^5.0.0",
"@swimlane/ngx-charts": "^6.1.0",
"@types/filesaver": "0.0.30",
"angular-chat": "^1.0.9",
"angular-datatables": "^4.2.0",
"angular-file-picker": "^1.2.0",
"angular-file-saver": "^1.1.3",
"angular-material": "^1.1.5",
"angularfire2": "5.0.0-rc.3",
"angularjs-bootstrap-datetimepicker": "^1.1.4",
"chart.js": "^2.7.2",
"chart.piecelabel.js": "^0.11.0",
"cloudboost": "^2.0.264",
"core-js": "^2.4.1",
"d3": "^4.11.0",
"datatables.net": "^1.10.16",
"datatables.net-buttons": "^1.4.2",
"datatables.net-buttons-dt": "^1.4.2",
"datatables.net-dt": "^1.10.16",
"datatables.net-select": "^1.2.3",
"datatables.net-select-dt": "^1.2.3",
"file-saver": "^1.3.3",
"firebase": "4.6.0",
"hammerjs": "^2.0.8",
"material-design-icons": "^3.0.1",
"md-date-picker": "^0.1.0",
"ng-chat": "^1.0.12",
"ng-snotify": "^4.0.0",
"ng2-charts": "^1.6.0",
"ng2-search-filter": "^0.3.1",
"ng4-loading-spinner": "^1.0.27",
"ngx-cookie-service": "^1.0.9",
"ngx-inactivity": "^1.0.1",
"ngx-loading": "^1.0.7",
"ngx-pagination": "^3.1.0",
"ngx-progress-bar": "0.0.10",
"rxjs": "^5.5.11",
"snazzy-info-window": "^1.1.0",
"time-ago-pipe": "^1.3.2",
"zone.js": "^0.8.14"
},
"devDependencies": {
"@angular/cli": "^1.4.1",
"@angular/compiler-cli": "^5.0.0",
"@angular/language-service": "^4.0.0",
"@types/datatables.net": "^1.10.6",
"@types/jasmine": "~2.5.53",
"@types/jasminewd2": "~2.0.2",
"@types/jquery": "^3.2.12",
"@types/node": "~6.0.60",
"codelyzer": "~3.0.1",
"jasmine-core": "~2.6.2",
"jasmine-spec-reporter": "~4.1.0",
"karma": "~1.7.0",
"karma-chrome-launcher": "~2.1.1",
"karma-cli": "~1.0.1",
"karma-coverage-istanbul-reporter": "^1.2.1",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.1.2",
"ts-node": "~3.0.4",
"tslint": "~5.3.2",
"typescript": "2.4.2"
}
}
答案 0 :(得分:2)
我也遇到了MAT-TREE的相同错误
通过更新Angular Material @ package.json并删除节点模块和npm安装来解决。
From "@angular/material": "^6.2.0" to=> "@angular/material": "^6.4.2"