[英]How to ensure JSON object structure using type and interface in Angular?
我的 angular 項目中有幾個模塊和組件。 我創建了一個路由器配置文件,以便於處理路由,它包含 JSON 數據,如下所示。
路由器.config.ts
{
branch:{
list:'/app/branch/list',
add:'/app/branch/add'
},
books:{
publish:'/app/books/publish',
manage:'/app/books/manage'
}
}
因此,基於配置,路由將很容易。
<a routerLink="branch.list">
Branch list
</a>
<a routerLink="books.publish">
Publish book
</a>
我想驗證路由器配置對象。 如何通過使用interface
和type
來驗證這一點?
interface Route {
[key: string]:
{ [key: string]: string }
}
const Routes :Route[] = [
{
branch:{
list:'/app/branch/list',
add:'/app/branch/add'
},
books:{
publish:'/app/books/publish',
manage:'/app/books/manage'
}
}
];
像這樣定義路由器項目:
interface RouterItem {
list: string;
add: string;
}
這個 function 用於實例驗證:
function isInstanceOfRouterItem(obj: any){
return 'list' in obj && 'add' in obj;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.