我創建了一個虛擬服務來回傳一些虛擬資料,然后由組件 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"
}])
}
}
uj5u.com熱心網友回復:
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
}
}
)}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/495086.html
