无法绑定到'dataSource',因为它不是'mat-tree'的已知属性

时间:2018-03-06 12:56:50

标签: angular-material2

出于某种原因,我收到此错误

  

无法绑定到'dataSource',因为它不是'mat-tree'的已知属性。

我不知道如何修复

我尝试使用角度素材2 6.0.0-beta.3,这个版本第一次有树视图组件,任何修复此错误的帮助都会有所帮助。

的package.json

  "dependencies": {
    "@angular/animations": "^6.0.0-beta.5",
    "@angular/cdk": "^6.0.0-beta.4",
    "@angular/common": "^6.0.0-beta.5",
    "@angular/compiler": "^6.0.0-beta.5",
    "@angular/core": "^6.0.0-beta.5",
    "@angular/flex-layout": "^2.0.0-beta.12",
    "@angular/forms": "^6.0.0-beta.5",
    "@angular/http": "^6.0.0-beta.5",
    "@angular/material": "^6.0.0-beta.3",
    "@angular/material-moment-adapter": "^6.0.0-beta.5",
    "@angular/platform-browser": "^6.0.0-beta.5",
    "@angular/platform-browser-dynamic": "^6.0.0-beta.5",
    "@angular/router": "^6.0.0-beta.5",
    "@types/three": "^0.89.6",
    "core-js": "^2.4.1",
    "hammerjs": "^2.0.8",
    "moment": "^2.20.1",
    "ng2-truncate": "^1.3.11",
    "orbit-controls-es6": "^1.0.10",
    "rxjs": "^5.5.6",
    "stats.js": "^0.17.0",
    "three": "^0.89.0",
    "three-orbit-controls": "^82.1.0",
    "zone.js": "^0.8.19"
  },

app.component.html

<mat-tree [dataSource]="data" [treeControl]="tc">
    <mat-tree-node *matTreeNodeDef="let node" matTreeNodeToggle>
        <li>
            <div>
                <button mat-icon-button disabled>
          <mat-icon>
            remove
          </mat-icon>
        </button>
        {{node.name}}
            </div>
        </li>
    </mat-tree-node>

    <mat-nested-tree-node *matTreeNodeDef="let node; when: hasChild">
        <li>
            <div class="mat-tree-node">
                <button mat-icon-button matTreeNodeToggle>
          <mat-icon>
            {{tc.isExpanded(node) ? 'expand_more' : 'chevron_right'}}
          </mat-icon>
        </button> {{node.name}}
            </div>
            <ul [hidden]="!tc.isExpanded(node)">
                <ng-container matTreeNodeOutlet></ng-container>
            </ul>
        </li>
    </mat-nested-tree-node>
</mat-tree>

app.component.ts

import { Component, OnInit } from '@angular/core';
import { TreeControl, NestedTreeControl, FlatTreeControl } from '@angular/cdk/tree';
import { of } from 'rxjs/observable/of';

interface TestData {
  name: string;
  // level: number;
  children?: TestData[];
}

// const GetLevel = (node: TestData) => node.level;
// const IsExpandable = (node: TestData) => node.children && node.children.length > 0;
const GetChildren = (node: TestData) => of(node.children);
// const TC = new FlatTreeControl(GetLevel, IsExpandable);
const TC = new NestedTreeControl(GetChildren);


@Component({
  selector: 'app-browser',
  templateUrl: './browser.component.html',
  styleUrls: ['./browser.component.css']
})
export class BrowserComponent implements OnInit {

  tc = TC;
  data = [
    {
      name: 'a',
      children: [
        { name: 'g' },
        {
          name: 'b',
          children: [
            { name: 'e' },
            { name: 'f' }
          ]
        },
        {
          name: 'c',
          children: [
            { name: 'd' }
          ]
        }
      ]
    }];

  hasChild(_: number, node: TestData) {
    console.log(node);
    return node.children != null && node.children.length > 0;
  }

  constructor() { }

  ngOnInit() {
  }

}

2 个答案:

答案 0 :(得分:7)

我认为您忘记了在模块中导入MatTreeModule

import {MatTreeModule} from '@angular/material/tree';

也不要忘记在装饰器中提及

imports: [
  MatTreeModule
],

答案 1 :(得分:2)

我遇到了同样的问题,解决方案是在app.module导入中包含MatTreeModule

希望有所帮助!!!