如何从角度6的另一个类调用一个类中的函数? [英] How do I call a function in a class from another class in Angular 6?

查看:53
本文介绍了如何从角度6的另一个类调用一个类中的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的代码:

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);
    });
  }
}

我正在尝试从HomeComponent调用radioFilter()。我尝试的内容:

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

我有点想不起来了,因此任何建议都非常感谢。谢谢!

推荐答案

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

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

声明前使用的类"UserDataSource"

您的数据源不是组件模板中的ViewChild。它只是一个对象(没有html模板)。您得到的错误是TypeScript中的批注是在编译/传输时处理的。但是UserDataSource类在HomeComponent下面声明。你在它申报之前就已经在使用它了。您可以将其放在HomeComponent之上,但最好将其放在新文件中并导入。但这不是解决办法。

可能的解决方案

我不明白为什么不能直接调用RadiFilter方法。 它是UserDataSource的公共方法,在HomeComponent中有一个名为DataSource的实例化对象。只要确保不在构造函数中调用它即可。成员变量在调用构造函数之后进行处理。但是imho你可以直接调用dataSource.radioFilter()

这篇关于如何从角度6的另一个类调用一个类中的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆