【问题标题】:Argument of type 'Subscription' is not assignable to parameter of type“订阅”类型的参数不能分配给类型的参数
【发布时间】:2019-11-08 20:26:04
【问题描述】:

我正在尝试将数据从数据库中提取到我的angular 材料matdatatable。但在 ts 中,我收到此错误:“订阅”类型的参数不可分配给 ReservationList[] 类型的参数。 类型“订阅”缺少 ReservationList[] 类型中的以下属性:长度、弹出、推送、连接以及另外 26 个。

这是我的数据表组件.ts

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
}

请指导我如何解决这个问题并将我的数据放到表格中?

谢谢

【问题讨论】:

  • 我在这一行得到这个错误顺便说一句:this.dataSource = new MatTableDataSource(this.serv.refreshList());

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


【解决方案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");
 }

【讨论】:

    【解决方案2】:

    您的服务文件中的内容有误。试试这个代码:

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

    其他一切都必须保持不变。

    【讨论】:

      猜你喜欢
      • 2020-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-05
      相关资源
      最近更新 更多