![](/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.