简体   繁体   English

打字稿-导入中不包含名称空间

[英]typescript - namespaces not included on import

I have an angular application, written in typescript whose config registration was just getting out of control, and huge - it took up about 3 pages. 我有一个angular应用程序,用typescript编写,其config注册刚刚失控,而且很大-它占用了大约3页。

So I broke it up into multiple config(fn) sections, and moved as much logic out of that page as I could and encapsulated specific behaviors into their own classes; 因此,我将其分解为多个config(fn)部分,并尽可能地将逻辑移出该页面,并将特定行为封装到自己的类中。 it seemed like the smart way to go about cleaning it up. 似乎是清理它的明智之举。

each of the new classes looks something like this; 每个新类都看起来像这样;

namespace config {
    export class http {
       constructor($httpProvider: ng.IHttpProvider){
           // $httpProvider configuration
       }
    }
}

so back in my main.ts , the file that creates my module and registers everything, I import them over. 回到我的main.ts文件中,该文件创建我的module并注册所有内容,然后将它们导入。

import { http } from './config/http';
import { router } from ./config/router';
// etc.

but the namespace doesn't seem to be a part of this. 但是名称空间似乎并不是其中的一部分。 I cannot call them such as... 我不能这样称呼他们...

config(config.http)
config(config.router)
// etc.

I've had to instead give them new alias when bringing them in; 当引入它们时,我不得不给它们新的别名。

import { http as configHttp } from './config/http';
import { router as configRouter } from './config/router';
// etc.

Why is this? 为什么是这样? Is there anything I can do to keep the namespace definition intact and use the simpler method I was aiming for? 我可以做些什么来保持名称空间定义完整,并使用我想要的更简单的方法?

The namespace you have is redundant if you're using import statements. 如果使用import语句,则拥有的名称空间是多余的。

Here: 这里:

import { http } from './config/http';

You're asking only for the http class in that file so that's what you're getting. 您只要求该文件中的http类,这就是您所得到的。

Also, if anything it should be module config and not namespace config . 另外,如果有的话,它应该是module config而不是namespace config
You should read Namespaces and Modules and more precisely Needless Namespacing . 您应该阅读命名空间和模块 ,更确切地说是不必要的命名空间

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

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