我有一個 Angular 組件的 Input() 變數的問題。它會在頁面模板檔案上引發與型別相關的錯誤。我有兩個界面,一個頁面和一個組件來使錯誤發生。
這兩個介面是Bike和BikeMinwhere Bikeextends BikeMin(和其他介面):
export interface Bike extends BikeRemote, BikeMin, BikeLocal {}
我有BikeFeaturePickerComponent一個 ngModel 型別的組件BikeMin:
export class BikeFeaturePickerComponent {
@Output("bikeChange")
public bikeChange: EventEmitter<BikeMin> = new EventEmitter<BikeMin>();
@Input("bike")
public bike: BikeMin;
ngOnInit(): void {}
constructor(public featuresService: FeaturesService, public bikeInfoService: BikeInfoService) {}
}
我在具有公共屬性BikeFeaturePickerComponent的頁面上使用組件。detail.page.ts :DetailPagebike: Bike
export class DetailPage implements OnInit {
public bike: Bike;
...
}
detail.page.html :
<app-bike-feature-picker [(bike)]="bike"></app-bike-feature-picker>
現在錯誤發生在detail.page.html 上。它說我不能將型別Bike的變數分配給型別的變數,BikeMin盡管Bike具有BikeMin. 在函式內部,我可以將型別的變數分配給型別Bike的引數BikeMin。但是當從@angular/core 使用 Input() 時,我收到以下錯誤:

在運行時 ( ionic serve) 也不會發生錯誤。盡管有編譯器錯誤,代碼仍然可以執行。也沒有錯誤ionic build。可能該錯誤僅由 Visual Studio 代碼引起。但我怎樣才能擺脫它呢?
感謝您的任何建議。
uj5u.com熱心網友回復:
當您使用雙向系結時,型別的轉換同時進行,即Bike -> BikeMin和BikeMin -> Bike。我猜這BikeMin -> Bike是導致問題的原因
您可能需要在 DetailPage 中使用聯合型別
public bike: Bike | BikeMin;
uj5u.com熱心網友回復:
感謝https://stackoverflow.com/a/70011876/11063209
它在將輸出型別更改為更通用的型別時有效Bike。因為特定的介面BikeMin不能Bike在輸出上轉換為通用介面:
@Output("bikeChange")
public bikeChange: EventEmitter<Bike> = new EventEmitter<Bike>();
@Input("bike")
public bike: BikeMin;
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/360632.html
