首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角5物料表未从服务部门获得数据

角5物料表未从服务部门获得数据
EN

Stack Overflow用户
提问于 2017-09-27 20:36:14
回答 2查看 3.4K关注 0票数 1

我试图使用带有资料表的角(5)来简单地显示来自MySQL数据库的数据。我的代码编译成功,出现了一个列名和分页按钮的表,但是表中没有显示数据。我知道API调用正在正确地返回数据,并且我知道服务正在调用API (我可以在浏览器的网络检查器中看到正确的数据)。的浏览器中显示了一个错误。

代码语言:javascript
复制
TypeError: _co.unitTypeDatabase.data is undefined
Stack trace:
View_UnitTypeComponent_0/<@ng:///AppModule/UnitTypeComponent.ngfactory.js:136:9 ...

该错误似乎是指html代码中引用unitTypeDatabase.data.length的部分。如果这是未定义的,那么它将解释为什么表中也没有数据。我无法理解的是为什么是未定义的。我认为这与如何/何时/在哪里创建new UnitTypeDatabase有关,但考虑到服务正在成功地被调用,我不知道从哪里开始。下面是我的代码:

unit-type.component.ts

代码语言:javascript
复制
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));
  }
}

unit-type.component.html

代码语言:javascript
复制
<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>

unit-type.ts

代码语言:javascript
复制
export class UnitType {
    constructor(
        public unitTypeID: number,
        public unitTypeName: string
    ){}
}

unit-type.service.ts

代码语言:javascript
复制
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的问题和答案相匹配,但我肯定遗漏了什么。希望这是一个明显的,简单的错误或误解,有人可以很快发现。谢谢!

编辑完整的堆栈跟踪是:

代码语言:javascript
复制
ERROR TypeError: _this._unitTypeDatabase.data is undefined
Stack trace:
[208]/UnitTypeDataSource.prototype.connect/<@http://localhost:4200/main.bundle.js:93:17
MapSubscriber.prototype._next@http://localhost:4200/vendor.bundle.js:51417:22
Subscriber.prototype.next@http://localhost:4200/vendor.bundle.js:37214:13
OuterSubscriber.prototype.notifyNext@http://localhost:4200/vendor.bundle.js:48573:9
InnerSubscriber.prototype._next@http://localhost:4200/vendor.bundle.js:117362:9
Subscriber.prototype.next@http://localhost:4200/vendor.bundle.js:37214:13
Subject.prototype.next@http://localhost:4200/vendor.bundle.js:26457:17
BehaviorSubject.prototype.next@http://localhost:4200/vendor.bundle.js:49978:9
UnitTypeDatabase/<@http://localhost:4200/main.bundle.js:122:78
SafeSubscriber.prototype.__tryOrUnsub@http://localhost:4200/vendor.bundle.js:37363:13
SafeSubscriber.prototype.next@http://localhost:4200/vendor.bundle.js:37310:17
Subscriber.prototype._next@http://localhost:4200/vendor.bundle.js:37250:9
Subscriber.prototype.next@http://localhost:4200/vendor.bundle.js:37214:13
MapSubscriber.prototype._next@http://localhost:4200/vendor.bundle.js:51423:9
Subscriber.prototype.next@http://localhost:4200/vendor.bundle.js:37214:13
onLoad@http://localhost:4200/vendor.bundle.js:53409:21
ZoneDelegate.prototype.invokeTask@http://localhost:4200/polyfills.bundle.js:6443:17
onInvokeTask@http://localhost:4200/vendor.bundle.js:4914:24
ZoneDelegate.prototype.invokeTask@http://localhost:4200/polyfills.bundle.js:6442:17
Zone.prototype.runTask@http://localhost:4200/polyfills.bundle.js:6242:28
ZoneTask/this.invoke@http://localhost:4200/polyfills.bundle.js:6496:28

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-10-05 23:16:11

问题在于单元类型.service.ts。它应该是:

代码语言:javascript
复制
getAllUnitTypes(): Observable<UnitType[]> {
    return this.http.get(this.unit_types_Url)
           .map(response => response.json() as UnitType[]);
}

我忽略了一个事实,即返回的JSON中没有数据对象,因此一旦删除,所有信息都是可见的,并在表中正确显示。

票数 0
EN

Stack Overflow用户

发布于 2017-09-27 23:30:49

首先,尝试在模板中使用elvis操作符?。如果只是模板问题,那么问题就会得到解决。

代码语言:javascript
复制
[length]="unitTypeDatabase?.data?.length"

原因是当视图被呈现时,还没有定义unitTypeDatabase,因为它只是在ngOnInit()中被初始化。看看这能不能解决你的问题。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46456808

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档