键入' void'不能指定类型'任何[]'

时间:2016-10-18 21:35:39

标签: angular kendo-grid

我在构造函数代码的后续行中遇到错误

this.data = this.getRisks(); . 

错误是

  

键入' void'不能指定为'任何[]'。

有人可以告诉我如何分配它。

import { Component, OnInit } from '@angular/core';
import { Risk } from './risk';
import { RiskService } from './risk.service';
import { GridModule, GridDataResult, PageChangeEvent } from '@progress/kendo-angular-grid';

@Component({
    moduleId: module.id,
    selector: 'rm-risks',
    templateUrl: '/app/risk-list.component.html',
    providers: [RiskService]
})

export class RiskListComponent implements OnInit {
    private gridView: GridDataResult;
    private data: any[];
    private pageSize: number = 50;
    private skip: number = 0;
    title = 'Risk List';
    risks: Risk[];

    constructor(private _riskService: RiskService) {
        this.data = this.getRisks();
        this.loadRisks();
     }

    protected pageChange(event: PageChangeEvent): void {
        this.skip = event.skip;
        this.loadRisks();
    }

    private loadRisks(): void {
        this.gridView = {
            data: this.data.slice(this.skip, this.skip + this.pageSize),
            total: this.data.length
        };
    }

    getRisks(): void {
        this._riskService.getRisks().then(risks => this.risks = risks);

    }

    ngOnInit(): void {
        this.getRisks();
    }
};

risk.service.ts

import { Injectable } from '@angular/core';
import { Risk } from './risk';
import { Risks } from './mock-risk';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/from';


@Injectable()
export class RiskService {

    getRisks(): Promise<Risk[]> {
        return Promise.resolve(Risks);
    }

}

1 个答案:

答案 0 :(得分:2)

由于getRisks()没有返回任何内容,只需调用它即可设置this.data

constructor(private _riskService: RiskService) {
    this.getRisks();
    this.loadRisks();
 }

如果需要在数据可用之前定义this.data = [],可以设置cd