简体   繁体   English

在打字稿上使用接口推送项目

[英]Push item with interfaces on typescript

I need some help about interfaces in typescript 我需要有关打字稿中的接口的帮助

I have 3 interface: 我有3个界面:

export interface HomeMenu {
    [name: string]: MenuItem;
}

export interface MenuItem {
    title: string;
    route: string;
    homeMenu?: HomeMenu;
}

export interface Menu {
    homeMenu: HomeMenu;
}

Now i do this : 现在我这样做:

var json: Menu = {
    "homeMenu": {
        "aname1": {
            "title": "text",
            "route": "myroute"
        },
        "aname2": {
            "title": "text",
            "route": "myroute",
            "homeMenu": {
                "aname21": {
                    "title": "text",
                    "route": "myroute"
                },
                "aname22": {
                    "title": "text",
                    "route": "myroute"
                }
            }
        },
        "aname3": {
            "title": "text",
            "route": "myroute"
        }
    }
}
json.homeMenu["aname2"].title = "myTitle";

But now how I can push a new item in the HomeMenu ? 但是现在我如何在HomeMenu中推送新项目? I tried to extend the HomeMenu interfaces with Array but I have an error about the index signature, Array want a number for Index(name) and not a string 我试图用Array扩展HomeMenu接口,但是索引索引出现错误,Array想要一个数字作为Index(name)而不是字符串
I have checked the Typescript, some post and i haven't found an answer Or maybe another solution to have a push function on homeMenu object ? 我检查了打字稿,有些帖子,但我还没有找到答案?或者也许是另一种在homeMenu对象上具有推功能的解决方案? If someone can help me Thanks in advance 如果有人可以帮助我

How about: 怎么样:

json.homeMenu["aname4"] = {
    title: "title 4",
    route: "route4"
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM