简体   繁体   English

在TypeScript和命名空间中使用Angular

[英]Using Angular with TypeScript and Namespaces

I'm in the process of building a large-scale application and I'm trying to evaluate TypeScript and Angular as the base technologies to use. 我正在构建一个大型应用程序,并且正在尝试将TypeScript和Angular评估为要使用的基本技术。

Unfortunately, I'm having an issue with namespacing my own TypeScript files once I begin using Angular. 不幸的是,一旦开始使用Angular,我在命名自己的TypeScript文件时就会遇到问题。

Taking the example of the Heroes tutorial , I tried to make it match how I would see my own application being structured. Heroes教程为例,我试图使其与我看到自己的应用程序结构化的方式匹配。

- app 
-- boot.ts

- Heroes
-- Components
--- HeroDetail.ts
--- HeroList.ts

-- Interfaces
--- IHero.ts

Within this structure, I then attempted to use namespaces for each logical area in order to group them. 然后,在此结构中,我尝试为每个逻辑区域使用名称空间,以便对其进行分组。 This resulted in IHero looking like this (for example): 这导致IHero看起来像这样(例如):

namespace Heroes.Interfaces
{
  export interface IHero
  {
    Id: number;
    Name: string;
  }
}

Building the project in this structure works well until I attempt to add Angular into the mix by way of: import {Component} from 'angular2/core'; 在我尝试通过以下方式将Angular添加到混合中之前,以这种结构构建项目效果良好: import {Component} from 'angular2/core'; When this happens, the file loses all references to other code in the same namespace and there's no way to make it discover them. 发生这种情况时,文件将丢失对同一命名空间中其他代码的所有引用,并且无法使其发现它们。

I've seen this question asked about the same issue. 我已经看到这个问题问过同样的问题。 There, the answer mentions "work arounds". 在那里,答案提到“解决方法”。 For this reason I'm thinking it's POSSIBLE, but more than that I'm wondering why it ISN'T at the minute? 由于这个原因,我认为这是可能的,但不仅如此,我还在想为什么现在不知道呢? Is this a bug or by design? 这是错误还是设计使然? I've been considering raising it on the TS GitHub but I would like to be a little better informed before going down that route. 我一直在考虑在TS GitHub上进行开发,但是在沿这条路线走前,我希望有所了解。

Thanks for any advice! 感谢您的任何建议!

Is this a bug or by design? 这是错误还是设计使然?

This is by design. 这是设计使然。 Don't use namespaces if you are using file modules. 如果使用文件模块,请不要使用名称空间。 Each file is already a module . 每个文件已经是一个模块 And your whole project needs a module loader. 您的整个项目都需要一个模块加载器。

Read: Angular 2 Modules vs JavaScript Modules — Angular 2 Architectural Documentation 阅读: Angular 2模块与JavaScript模块— Angular 2建筑文档

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

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