类型“ Subscription”的参数不能分配给类型的参数

时间:2019-11-08 11:32:03

标签: javascript angular typescript angular-material angular-datatables

我正在尝试从数据库中获取数据到我的angular资料matdatatable。但是在ts中,会出现以下错误:无法将类型'Subscription'的参数分配给类型ReservationList[]的参数。       类型'Subscription'缺少类型ReservationList[]中的以下属性:length,pop,push,concat和另外26个。

这是我的数据表组件。

import { Component, OnInit, ViewChild } from '@angular/core';
import {MatPaginator} from '@angular/material/paginator';
import {MatSort} from '@angular/material/sort';
import {MatTableDataSource} from '@angular/material/table';
import { ReservationList } from '../models/reservation-list.model';
import { ReservationService } from '../services/reservation.service';

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

  displayedColumns: string[] = ['roomName', 'name', 'progress', 'color'];
  dataSource: MatTableDataSource<ReservationList>;

  @ViewChild(MatPaginator, {static: true}) paginator: MatPaginator;
  @ViewChild(MatSort, {static: true}) sort: MatSort;

  constructor(private serv: ReservationService) {


  }

  ngOnInit() {
    this.dataSource.paginator = this.paginator;
    this.dataSource.sort = this.sort;
    this.dataSource = new MatTableDataSource(this.serv.refreshList());
  }

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

    if (this.dataSource.paginator) {
      this.dataSource.paginator.firstPage();
    }
  }
}

这是我的服务:

    import { Injectable } from '@angular/core';
import {ReservationList} from '../models/reservation-list.model';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class ReservationService {

  reservationlist: ReservationList[];

  constructor(private _http: HttpClient) { }

  refreshList(){
    return this._http.get("https://localhost:44389/api/reservations").subscribe(res => this.reservationlist = res as ReservationList[]);
 }
}

这是我的app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatInputModule, MatNativeDateModule } from '@angular/material';
import { SearchComponent } from './search/search.component';
import { ListComponent } from './list/list.component'
import {MatDatepickerModule} from '@angular/material/datepicker';
import {MatSelectModule} from '@angular/material/select';
import {MatTableModule} from '@angular/material/table';
import {MatButtonModule} from '@angular/material/button';
import {MatCardModule} from '@angular/material/card';
import { HttpClientModule } from '@angular/common/http';
import { MatPaginatorModule } from '@angular/material/paginator';
import { MatSortModule } from '@angular/material/sort';
import { MattabledataComponent } from './mattabledata/mattabledata.component';

@NgModule({
  declarations: [
    AppComponent,
    SearchComponent,
    ListComponent,
    MattabledataComponent,
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    BrowserAnimationsModule,
    FormsModule,
    ReactiveFormsModule,
    MatInputModule,
    MatDatepickerModule,
    MatNativeDateModule,
    MatSelectModule,
    MatTableModule,
    MatButtonModule,
    MatCardModule,
    HttpClientModule,
    MatPaginatorModule,
    MatSortModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

这是我的预订模式:

    export class ReservationList {
    hotelId: number
    currency: string
    roomName: string 
    roomId: number 
    boardName: string
    checkInDate: Date
    duration: number
    numberOfAd:  number 
    numberOfChd:  number 
    minAdtAge:  number 
    ch1AgeMin:  number 
    ch1AgeMax:  number 
    ch2AgeMin:  number 
    ch2AgeMax:  number 
    ch3AgeMin:  number 
    ch3AgeMax:  number 
    price:  number
    PayDate: string
}

请指导我如何解决此问题并将数据发送到表中?

谢谢

2 个答案:

答案 0 :(得分:1)

问题是您无法在异步订阅调用中返回值。它只会返回您可以取消订阅的订阅。

做一些类似的事情

 this.dataSource = new MatTableDataSource([]);

this.serv.refreshList().subscribe(result => {
  this.dataSource.data = [...result]
}) 

服务功能

refreshList(){
    return this._http.get<ReservationList[]>("https://localhost:44389/api/reservations");
 }

答案 1 :(得分:0)

服务文件中的内容错误。尝试以下代码:

import { Injectable } from '@angular/core';
import {ReservationList} from '../models/reservation-list.model';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class ReservationService {

  reservationlist: ReservationList[];

  constructor(private _http: HttpClient) { }

  refreshList(){
    return this._http.get("https://localhost:44389/api/reservations")
 }
}

在您的组件文件中执行以下操作:

 import { Component, OnInit, ViewChild } from '@angular/core';
    import {MatPaginator} from '@angular/material/paginator';
    import {MatSort} from '@angular/material/sort';
    import {MatTableDataSource} from '@angular/material/table';
    import { ReservationList } from '../models/reservation-list.model';
    import { ReservationService } from '../services/reservation.service';

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

      displayedColumns: string[] = ['roomName', 'name', 'progress', 'color'];
      dataSource: MatTableDataSource<ReservationList>;

      @ViewChild(MatPaginator, {static: true}) paginator: MatPaginator;
      @ViewChild(MatSort, {static: true}) sort: MatSort;
apiResponse: ReservationList[] = [];


      constructor(private serv: ReservationService) {


      }

      ngOnInit() {
this.serv.refreshList.subscribe((res: any) => this.apiResponse = res as ReservationList[]);
        this.dataSource.paginator = this.paginator;
        this.dataSource.sort = this.sort;
        this.dataSource = new MatTableDataSource(this.apiResponse);
      }

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

        if (this.dataSource.paginator) {
          this.dataSource.paginator.firstPage();
        }
      }
    }

其他所有内容都必须保持不变。