Angular如何在整个应用程序中共享界面 [英] Angular how to share interfaces across the app

查看:86
本文介绍了Angular如何在整个应用程序中共享界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个看起来像这样的interfaces.ts文件:

So I have a interfaces.ts file which looks something like this:

export interface Auction{ $key?:string; $auctioneer:string;  ... }
export interface Item{ $key?:string; condition?:string; description?:string; ...}
export interface Bid{  $key?:string;  amount:number;  auction:string; ...}
and so on  ...

现在,我可以通过调用以下命令将这些接口导入每个组件/服务中:

Now I can import those interfaces in each component/service by calling:

import { Auction,Item,Bid } from './interfaces';

但是如何导入 interfaces.ts 文件,以便所有接口都可以在全球范围内使用?

But how can I import the interfaces.ts file so all interfaces will be available globally?

推荐答案

您将必须在要使用这些接口的每个组件/服务中导入interfaces.ts.

You will have to import interfaces.ts in every component/service where you're going to want to use those interfaces.

您能做的最接近的事情(也许可以节省一些写作)是:

The closest thing you can do (which saves some writing, maybe) is:

    import * as foo from './interfaces';

    var item: foo.Item;
    var auction: foo.Auction;
    var bid: foo.Bid;

如果不导入interfaces.ts,则根本找不到接口的名称.

If you do not import interfaces.ts, it can simply not find the names of the interfaces.

这篇关于Angular如何在整个应用程序中共享界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆