这是我的代码:

import { Component, OnInit, ViewChild } from '@angular/core';
import { AuthService } from '../core/auth.service';
import { MatRadioButton, MatPaginator, MatSort, MatTableDataSource } from '@angular/material';
import { SelectionModel } from '@angular/cdk/collections';
import { OrdersService } from '../orders.service';

export interface DataTableItem {
  ordersn: string;
  order_status: string;
  update_time: number;
}

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss']
})

export class HomeComponent implements OnInit {
  radioValue: number;
  dataSource = new UserDataSource(this.orderService);
  selection = new SelectionModel<any>(true, []);

  // Sorting and pagination
  @ViewChild(MatSort) sort: MatSort;
  @ViewChild(MatPaginator) paginator: MatPaginator;

  // Columns displayed in the table. Columns IDs can be added, removed, or reordered.
  displayedColumns = ['ordersn', 'order_status', 'update_time'];

  // Filter
  applyFilter(filterValue: string) {
    this.dataSource.filter = filterValue.trim().toLowerCase();
  }

  // Whether the number of selected elements matches the total number of rows.
  isAllSelected() {
    const numSelected = this.selection.selected.length;
    const numRows = this.dataSource.data.length;
    return numSelected === numRows;
  }

  // Selects all rows if they are not all selected; otherwise clear selection.
  masterToggle() {
    this.isAllSelected() ?
      this.selection.clear() :
      this.dataSource.data.forEach(row => this.selection.select(row));
  }

  constructor(public auth: AuthService, private orderService: OrdersService) {
  }

  onSelectionChange(radioSelection: MatRadioButton) {
    this.radioValue = radioSelection.value;
    console.log(this.radioValue);
  }

  ngOnInit() {
    this.dataSource.sort = this.sort;
    this.dataSource.paginator = this.paginator;
  }
}

export class UserDataSource extends MatTableDataSource<any> {
  constructor(private orderService: OrdersService) {
    super();

    this.orderService.GetOrdersList().subscribe(d => {
      this.data = d.orders;
    });
  }

  radioFilter() {
    const array = [];

    this.orderService.GetOrdersList().subscribe(d => {
      for (const entry of d.orders) {
        if (entry.order_status === 'READY_TO_SHIP') {
          array.push(entry);
        }
      }

      this.data = array;

      console.log(array);
    });
  }
}


我正在尝试从radioFilter()呼叫HomeComponent。我尝试过的


@ViewChild中实现HomeComponent,但会出现此错误:在声明之前使用了类'UserDataSource'。
导入UserDataSource,然后将其添加到HomeComponent中的构造函数。我会收到此错误:未被捕获的错误:无法解析HomeComponent的所有参数


我有点想法了,因此任何建议都值得赞赏。谢谢!

最佳答案

出现未捕获的错误:无法解析HomeComponent的所有参数

首先,您的dataSource没有在ngModule中注册为可注入。
因此,不可能将其注入HomeComponent中的构造函数。
我也不认为您要这样做,因为ngMaterial-dataSources是有状态的,而injectables不应是有状态的。

声明前使用的类'UserDataSource'

您的dataSource不是组件模板中的ViewChild。它只是一个对象(没有html模板)。您得到的错误是typeScript中的注释是在编译/转换时处理的。但是UserDataSource类在HomeComponent下面声明。您在声明它之前就在使用它。您可以将其放在HomeComponent上方,但最好将其放在新文件中并导入。但这不是解决方案。

可能的解决方案

我不明白为什么您不能只调用radioFilter方法。
这是UserDataSource的公共方法,并且在HomeComponent中有一个实例化的对象,称为dataSource。只要确保不要在构造函数中调用它即可。调用构造函数后,将处理成员变量。但是恕我直言,您可以致电dataSource.radioFilter()

09-25 18:57