在Angular 9中通过API实现树形视图时失败

问题描述 投票:0回答:1

我正在尝试实现材料设计树视图,该视图完全从API加载数据。但是,我的实现抛出了错误。 HttpClient导入似乎很好,我没有弄错。我尝试修改该代码,但无济于事。

我的.scss文件:

.example-tree-progress-bar {
  margin-left: 30px;
}

.example-tree-invisible {
  display: none;
}

.example-tree ul,
.example-tree li {
  margin-top: 0;
  margin-bottom: 0;
  list-style-type: none;
}

我的.html文件:

<mat-tree [dataSource]="dataSource" [treeControl]="treeControl" class="example-tree">
  <!-- This is the tree node template for leaf nodes -->
  <mat-tree-node *matTreeNodeDef="let node" matTreeNodeToggle>
    <li class="mat-tree-node">
      <!-- use a disabled button to provide padding for tree leaf -->
      <button mat-icon-button disabled></button>
      {{node.name}}
    </li>
  </mat-tree-node>
  <!-- This is the tree node template for expandable nodes -->
  <mat-nested-tree-node *matTreeNodeDef="let node; when: hasChild">
    <li>
      <div class="mat-tree-node">
        <button mat-icon-button matTreeNodeToggle
                [attr.aria-label]="'toggle ' + node.name">
          <mat-icon class="mat-icon-rtl-mirror">
            {{treeControl.isExpanded(node) ? 'expand_more' : 'chevron_right'}}
          </mat-icon>
        </button>
        {{node.name}}
      </div>
      <ul [class.example-tree-invisible]="!treeControl.isExpanded(node)">
        <ng-container matTreeNodeOutlet></ng-container>
      </ul>
    </li>
  </mat-nested-tree-node>
</mat-tree>

我的.ts文件:

import {NestedTreeControl} from '@angular/cdk/tree';
import {Component} from '@angular/core';
import {MatTreeNestedDataSource} from '@angular/material/tree';
import { HttpClient, HttpHeaders } from '@angular/common/http';

/**
 * Food data with nested structure.
 * Each node has a name and an optional list of children.
 */
interface FoodNode {
  name: string;
  children?: FoodNode[];
}

/* const TREE_DATA: FoodNode[] = [
  {
    name: 'Fruit',
    children: [
      {name: 'Apple'},
      {name: 'Banana'},
      {name: 'Fruit loops'},
    ]
  }, {
    name: 'Vegetables',
    children: [
      {
        name: 'Green',
        children: [
          {name: 'Broccoli'},
          {name: 'Brussels sprouts'},
        ]
      }, {
        name: 'Orange',
        children: [
          {name: 'Pumpkins'},
          {name: 'Carrots'},
        ]
      },
    ]
  },
]; */



const TREE_DATA: FoodNode[] = [];



/**
 * @title Tree with nested nodes
 */
@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.scss'],
})
export class TreeNestedOverviewExample {
  treeControl = new NestedTreeControl<FoodNode>(node => node.children);
  dataSource = new MatTreeNestedDataSource<FoodNode>();

  constructor(private client: HttpClient) {
    this.dataSource.data = TREE_DATA;

  }

  ngOnInit() {
    const httpOptions = {
      headers: new HttpHeaders({ 'Content-Type': 'application/json' })
    };
    const API_URL = 'https://urlhere.com/gettreeview';
  this.client.get(API_URL, this.httpOptions).subscribe(
    (res) => { this.TREE_DATA.push(res);console.log('Res: ', res); },
  );
  }

  hasChild = (_: number, node: FoodNode) => !!node.children && node.children.length > 0;
}



我的错误:

core.js:5882 ERROR NullInjectorError: R3InjectorError(AppModule)[HttpClient -> HttpClient -> HttpClient]: 
  NullInjectorError: No provider for HttpClient!
main.ts:12 NullInjectorError: R3InjectorError(AppModule)[HttpClient -> HttpClient -> HttpClient]: 
  NullInjectorError: No provider for HttpClient!

我在做什么错?


UPDATE

我的JSON,可从API中获取:

[
   {
      "id": 1,
      "name": "- Admin",
      "children": [
         {
            "id": 2,
            "name": "Jimmy"
         },
         {
            "id": 3,
            "name": "Tom"
         }
      ]
   },
   {
      "id": 4,
      "name": "- Users",
      "children": [
         {
            "id": 5,
            "name": "Scott"
         },
         {
            "id": 6,
            "name": "John"
         }
      ]
   },
   {
      "id": 7,
      "name": "- Developer",
      "children": [
         {
            "id": 8,
            "name": "Robert"
         },
         {
            "id": 9,
            "name": "Scarlett"
         },
         {
            "id": 10,
            "name": "Johnson"
         }
      ]
   }
]
angular typescript tree treeview material-design
1个回答
1
投票

您缺少模块中的HttpClientModule导入。

将以下内容添加到app.module.ts:

import { HttpClientModule } from '@angular/common/http'; 

之后在导入部分中添加,例如

 imports:[HttpClientModule,  ]

请参见工作Angular 9 Stackblitz

© www.soinside.com 2019 - 2024. All rights reserved.