![](/img/trans.png)
[英]Angular4 Typescript; Property 'data' does not exist on type 'any[]'
[英]Property 'data' does not exist on type 'any[]'
我创建了一个虚拟服务来返回一些虚拟数据,然后由组件 ts 文件调用它。 但我得到了以下错误。
发现几个帖子有类似的错误,但仍然不知道如何纠正我的错误。
错误:
错误:src/app/roaster-load/file-load-init/file-load-init.component.ts:60:19 - 错误 TS2339:“任何 []”类型上不存在属性“数据”。
文件加载init.component.ts:
`
import { NgbModal, ModalDismissReasons, NgbModalOptions } from '@ng-bootstrap/ng-bootstrap';
import { BaseComponent } from '../../common/scripts/baseComponent';
import { WINDOW } from '../../common/scripts/window.service';
import { Component, OnInit} from '@angular/core';
import { HttpClientModule, HttpClient, HttpHandler } from '@angular/common/http';
import { UploadService } from '../UploadService/upload.service';
import { InvalidSOEIDModel } from '../Uploadmodel/uploadmodel';
import { Subscription } from 'rxjs';
@Component({
selector: 'app-file-load-init',
templateUrl: './file-load-init.component.html',
styleUrls: ['./file-load-init.component.css']
})
export class FileLoadInitComponent implements OnInit {
InvalidSOEIDCount: any;
InvalidSOEIDModel:InvalidSOEIDModel[];
ngOnInit(): void {
}
constructor(private http: HttpClient,
private UploadService: UploadService,) {}
OnUpload(){
this.GetSOEIDVerifyData();
};
private GetSOEIDVerifyData(){
this.UploadService.UploadDataReturn()
.subscribe((res: any[]) => {
console.log(res);
if (res.data) {
this.InvalidSOEIDCount = res.data
}
}
)}
}
上传.service.ts:
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { InvalidSOEIDModel } from '../Uploadmodel/uploadmodel'
import {of} from 'rxjs';
@Injectable()
export class UploadService {
UploadDataReturn(): Observable<InvalidSOEIDModel[]> {
return of ( [
{
SOEID: "AAAAA"
},
{
SOEID: "BBBBB"
}])
}
}
private GetSOEIDVerifyData(){
this.UploadService.UploadDataReturn()
.subscribe((res: InvalidSOEIDModel[]) => {
//Replaced any with InvalidSOEIDModel
//You can just write res as well it work , no need to mention type
//Your response is a array of InvalidSOEIDModel so response does't have
// any property of data but by writing res.data you are trying to acces
//property that does not exist
if (res.data) {
this.InvalidSOEIDCount = res.data //This is wrong
this.InvalidSOEIDCount = res.length // This is correct
}
}
)}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.