当内部模块已声明导入时,导入“找不到模块" [英] Import 'cannot find module' when internal module has imports declared
问题描述
我有这个命名空间
namespace Validation {
export function Func1() {
// code
}
export function Func2() {
// code
}
}
我可以导入我的app.ts
:
import Validations = Validation;
但是当我想引用Validation
名称空间中的某些模块
But when I want to reference some modules in my Validation
namespace
import {Request, Response} from 'express';
var jwt = require('jsonwebtoken');
var express = require('express');
import {Config} from './../config';
namespace Validation {
export function Func1() {
// code
}
export function Func2() {
// code
}
}
然后在我的app.ts
中的import Validations = Validation;
给我一个错误cannot find namespace Validation
.
So then import Validations = Validation;
in my app.ts
giving me an error cannot find namespace Validation
.
为什么会这样?有什么想法要解决吗?
Why it is happened? Any thoughts how to fix?
更新1:如果我将导入放在命名空间之后,则会出现错误Import declaration in a namespace cannot import a module
:
UPDATE 1 : In case if I put imports after namespace I am getting an error Import declaration in a namespace cannot import a module
:
namespace Validation {
import {Request, Response} from 'express'; //Error: Import declaration in a namespace cannot import a module
var jwt = require('jsonwebtoken');
var express = require('express');
import {Config} from './../config'; //Error: Import declaration in a namespace cannot import a module
export function Func1() {
// code
}
export function Func2() {
// code
}
}
我的config.ts
只是一个简单的类:
my config.ts
is just a simple class:
export class Config {
public static get Secret():string { return 'stuff'; }
public static get Database():string { return 'mongodb://127.0.0.1:27019/test'; }
}
'express'
这是一个 npm软件包
更新2 我想我只是通过将配置文件包装到名称空间中来修复该配置文件:
UPDATE 2 I think I just fixed config by wrapping it in to namespace:
namespace Common {
export class Config { .. }
}
也将导入语句从此import {Config} from './config';
更改为以下内容:import Config = Common.Config;
,但尚未弄清楚如何解决'express'
的问题
Also changed import statement from this import {Config} from './config';
to this: import Config = Common.Config;
but haven't yet figure out how to fix 'express'
thing
推荐答案
之所以发生这种情况,是因为从您将顶级import或export语句放入文件的那一刻起,该文件就被视为外部模块本身.如果您使用的是内部模块(命名空间),建议您导入 inside 命名空间,这样就不会出现顶级的import或export语句.
This happens because from the moment you put a top-level import or export statement into a file, that file is treated as an external module itself. If you are using internal modules (namespaces), I suggest importing inside namespaces, so that there are no top-level import or export statements.
namespace Validation {
import Request = ...;
import Response = ...;
export function Func1() {
// code
}
export function Func2() {
// code
}
}
另一种方法是改为使用外部模块,但这需要模块加载系统,在许多情况下可能是多余的.
The other approach would be to use external modules instead, but that requires a module loading system, which might be superfluous in many cases.
现在,您正在混合使用内部和外部模块,不建议这样做.对于像这样的复杂结构案例,Typescript还远远不是一种成熟的语言.
Right now, you are mixing internal and external modules, which is not recommended. Regarding complex structural cases like this, Typescript is still very far from being a mature language.
这篇关于当内部模块已声明导入时,导入“找不到模块"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!