在Angular中,可以通过Service组件获取数据,并将数据传递给mat-table组件进行展示。下面是一个示例解决方法:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
  providedIn: 'root'
})
export class DataService {
  constructor(private http: HttpClient) { }
  getData() {
    return this.http.get('https://api.example.com/data');
  }
}
上述代码中,使用了HttpClient模块来发送HTTP请求获取数据。
import { Component, OnInit } from '@angular/core';
import { DataService } from '../data.service';
@Component({
  selector: 'app-table',
  templateUrl: './table.component.html',
  styleUrls: ['./table.component.css']
})
export class TableComponent implements OnInit {
  dataSource: any;
  constructor(private dataService: DataService) { }
  ngOnInit() {
    this.dataService.getData().subscribe((data) => {
      this.dataSource = data;
    });
  }
}
上述代码中,通过订阅getData方法返回的Observable对象来获取数据,并将数据赋值给dataSource变量。
  
  
    Name 
    {{ item.name }} 
   
  
    Age 
    {{ item.age }} 
   
  
   
   
上述代码中,使用mat-table组件来定义表头和数据行,并通过ng-container指定每一列的字段名和对应的数据。
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { HttpClientModule } from '@angular/common/http';
import { TableComponent } from './table/table.component';
@NgModule({
  declarations: [
    AppComponent,
    TableComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
 
上述代码中,将TableComponent添加到declarations中,并导入HttpClientModule以便在TableComponent中使用HttpClient模块。
通过以上步骤,就可以从Service组件获取数据,并将数据传递给mat-table组件进行展示。