【发布时间】: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