我在我的 Angular 專案中使用 typescript 試圖將 IInfoPage 介面分配給資料
export interface IInfoPage {
href: string;
icon: string;
routing: boolean;
order: number;
styleType: string;
}
public pageData: IInfoPage;
this.configService.PageData.subscribe((res) => {
this.pageData = res.SubMenu.find(data => location.pathname.includes('path'));
})
但是當我看到 this.pageData 警報并給我這個錯誤時:
Type 'ISubMenuType | undefined' is not assignable to type 'IInfoPageSubData'. Type 'undefined' is not assignable to type 'IInfoPageSubData'.
那么我該如何解決這個問題呢?
uj5u.com熱心網友回復:
Array.find()can 總是 return undefined,所以你需要檢查是否find回傳了結果。
const result = res.SubMenu.find(data => location.pathname.includes('path'));
if(result)
this.pageData = result;
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/378798.html
標籤:javascript 有角的 打字稿
下一篇:避免在輸入欄位中輸入點
