[英]How to ensure JSON object structure using type and interface in Angular?
I have a couple of modules and components in my angular project.我的 angular 项目中有几个模块和组件。 I have created a router config file for easy handling of routing and it contains JSON data as follows.我创建了一个路由器配置文件,以便于处理路由,它包含 JSON 数据,如下所示。
router.config.ts路由器.config.ts
{
branch:{
list:'/app/branch/list',
add:'/app/branch/add'
},
books:{
publish:'/app/books/publish',
manage:'/app/books/manage'
}
}
So based on configuration, the routing would be easy.因此,基于配置,路由将很容易。
<a routerLink="branch.list">
Branch list
</a>
<a routerLink="books.publish">
Publish book
</a>
I want to validate the router config objects.我想验证路由器配置对象。 How do I validate that by using interface
and type
?如何通过使用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'
}
}
];
define router Item like this:像这样定义路由器项目:
interface RouterItem {
list: string;
add: string;
}
and this function for instance validation:这个 function 用于实例验证:
function isInstanceOfRouterItem(obj: any){
return 'list' in obj && 'add' in obj;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.