2017-09-27 155 views
0

我想简单地显示使用角度(5)与材料表的MySQL数据库中的数据。我已成功编译的代码,并出现一个包含列名和分页按钮的表,但表中没有显示数据。我知道API调用正确返回数据,并且我知道该服务正在调用API(我可以在浏览器中的网络检查器中看到正确的数据)。有一个在角度5材料表没有从服务获取数据

TypeError: _co.unitTypeDatabase.data is undefined 
Stack trace: 
View_UnitTypeComponent_0/<@ng:///AppModule/UnitTypeComponent.ngfactory.js:136:9 ... 

这样看来,错误指的是在HTML代码中提到unitTypeDatabase.data.length部分浏览器中显示一个错误。如果这是未定义的,那么它可以解释为什么表中没有数据。我不明白的是为什么这是未定义的。我认为这与我在创建new UnitTypeDatabase时的方式/时间/地点有关,但考虑到该服务成功地被调用,我不知道该从哪里去。下面是我的代码有:

单位type.component.ts

import { Component, OnInit, ViewChild } from '@angular/core'; 
import { UnitType } from './unit-type'; 
import { UnitTypeService } from './unit-type.service'; 
import {DataSource} from '@angular/cdk/collections'; 
import {MdPaginator} from '@angular/material'; 
import {Observable} from 'rxjs/Observable'; 
import {BehaviorSubject} from 'rxjs/BehaviorSubject'; 

@Component({ 
    selector: 'app-unit-type', 
    templateUrl: './unit-type.component.html', 
    styleUrls: ['./unit-type.component.scss'], 
    providers: [UnitTypeService] 
}) 
export class UnitTypeComponent implements OnInit { 
    public displayedColumns = ['unitTypeID', 'unitTypeName']; 
    public unitTypeDatabase: UnitTypeDatabase | null; 
    public dataSource: UnitTypeDataSource | null; 

    @ViewChild(MdPaginator) paginator: MdPaginator; 

    constructor(private unitTypeService: UnitTypeService) {} 

    ngOnInit() { 
     this.unitTypeDatabase = new UnitTypeDatabase(this.unitTypeService); 
     this.dataSource = new UnitTypeDataSource(this.unitTypeDatabase, this.paginator); 
    } 
} 

export class UnitTypeDataSource extends DataSource<UnitType> { 
    constructor(private _unitTypeDatabase: UnitTypeDatabase, private _paginator: MdPaginator) { 
     super(); 
    } 

    connect(): Observable<UnitType[]> { 
    const displayDataChanges = [ 
     this._unitTypeDatabase.dataChange, 
     this._paginator.page 
    ]; 

    return Observable.merge(...displayDataChanges).map(() => { 
     const data = this._unitTypeDatabase.data.slice(); 

     const startIndex = this._paginator.pageIndex * this._paginator.pageSize; 
     return data.splice(startIndex, this._paginator.pageSize); 
    }) 
    } 

    disconnect() {} 
} 

export class UnitTypeDatabase { 
    /** Stream that emits whenever the data has been modified. */ 
    public dataChange: BehaviorSubject<UnitType[]> = new BehaviorSubject<UnitType[]>([]); 
    get data(): UnitType[] { return this.dataChange.value; } 

    constructor(unitTypeService: UnitTypeService) { 
     unitTypeService.getAllUnitTypes().subscribe(data => this.dataChange.next(data)); 
    } 
} 

单位type.component.html

<div class="example-container mat-elevation-z8"> 
    <md-table #table [dataSource]="dataSource"> 

    <!-- ID Column --> 
    <ng-container mdColumnDef="unitTypeID"> 
     <md-header-cell *mdHeaderCellDef> ID </md-header-cell> 
     <md-cell *mdCellDef="let row"> {{row.unitTypeID}} </md-cell> 
    </ng-container> 

    <!-- Name Column --> 
    <ng-container mdColumnDef="unitTypeName"> 
     <md-header-cell *mdHeaderCellDef> Name </md-header-cell> 
     <md-cell *mdCellDef="let row"> {{row.unitTypeName}} </md-cell> 
    </ng-container> 

    <md-header-row *mdHeaderRowDef="displayedColumns"></md-header-row> 
    <md-row *mdRowDef="let row; columns: displayedColumns;"></md-row> 
    </md-table> 

    <md-paginator #paginator 
      [length]="unitTypeDatabase.data.length" 
      [pageIndex]="0" 
      [pageSize]="25" 
      [pageSizeOptions]="[5, 10, 25, 100]"> 
    </md-paginator> 
</div> 

单位type.ts

export class UnitType { 
    constructor(
     public unitTypeID: number, 
     public unitTypeName: string 
    ){} 
} 

单位type.service.ts

import { Injectable } from '@angular/core'; 
import { UnitType } from './unit-type'; 
import { Headers, Http } from '@angular/http'; 
import { Observable } from 'rxjs/Observable'; 

@Injectable() 
export class UnitTypeService { 
    private unit_types_Url = 'api/unit-types'; // URL to web api 
    private headers = new Headers({'Content-Type': 'application/json'}); 

    constructor(private http: Http) { } 

    getAllUnitTypes(): Observable<UnitType[]> { 
    return this.http.get(this.unit_types_Url) 
      .map(response => response.json().data as UnitType[]); 
    } 
} 

我开始从材料的网站上的例子,但使用服务,不检索数据。我也试图匹配How to use md-table with services in Angular 4的问题和答案,但我一定错过了一些东西。希望这是一个明显的,简单的错误或误解,有人可以迅速发现。谢谢!

编辑 完整的堆栈跟踪是:

ERROR TypeError: _this._unitTypeDatabase.data is undefined 
Stack trace: 
[208]/UnitTypeDataSource.prototype.connect/<@http://localhost:4200/main.bundle.js:93:17 
[email protected]://localhost:4200/vendor.bundle.js:51417:22 
[email protected]://localhost:4200/vendor.bundle.js:37214:13 
[email protected]://localhost:4200/vendor.bundle.js:48573:9 
[email protected]://localhost:4200/vendor.bundle.js:117362:9 
[email protected]://localhost:4200/vendor.bundle.js:37214:13 
[email protected]://localhost:4200/vendor.bundle.js:26457:17 
[email protected]://localhost:4200/vendor.bundle.js:49978:9 
UnitTypeDatabase/<@http://localhost:4200/main.bundle.js:122:78 
[email protected]://localhost:4200/vendor.bundle.js:37363:13 
[email protected]://localhost:4200/vendor.bundle.js:37310:17 
[email protected]://localhost:4200/vendor.bundle.js:37250:9 
[email protected]://localhost:4200/vendor.bundle.js:37214:13 
[email protected]://localhost:4200/vendor.bundle.js:51423:9 
[email protected]://localhost:4200/vendor.bundle.js:37214:13 
[email protected]://localhost:4200/vendor.bundle.js:53409:21 
[email protected]://localhost:4200/polyfills.bundle.js:6443:17 
[email protected]://localhost:4200/vendor.bundle.js:4914:24 
[email protected]://localhost:4200/polyfills.bundle.js:6442:17 
[email protected]://localhost:4200/polyfills.bundle.js:6242:28 
ZoneTask/[email protected]://localhost:4200/polyfills.bundle.js:6496:28 

enter image description here

回答

0

问题出在unit-type.service.ts中。它应该是:

getAllUnitTypes(): Observable<UnitType[]> { 
    return this.http.get(this.unit_types_Url) 
      .map(response => response.json() as UnitType[]); 
} 

我错过了事实,有作为返回JSON的部分没有数据对象,所以一旦被删除的所有信息是可见的,在表中正确显示。

1

尝试使用Elvis操作符?模板开始。如果这只是模板问题,那应该解决您的问题。

[length]="unitTypeDatabase?.data?.length" 

原因是当视图呈现没有尚未定义,因为它是在唯一ngOnInit()初始化unitTypeDatabase。看看是否解决了你的问题。

+0

感谢您的提示,但不幸的是,并没有解决问题。和以前一样,我仍然在浏览器中看到相同的错误。 – Tim

+0

可以显示错误的完整堆栈跟踪吗? – amal

+0

我编辑了问题以显示完整的堆栈跟踪。 – Tim