typescript 打字稿“不是模块”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40302392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:58:15  来源:igfitidea点击:

Typescript 'is not a module' error

javascriptangulartypescript

提问by DaRoGa

I have an Angular2 project where i need to import a javascript file for use in my typescript.

我有一个 Angular2 项目,我需要在其中导入一个 javascript 文件以在我的打字稿中使用。

I have a javascript file in app/js/d3gantt.js which contains a single function of gantt = function().

我在 app/js/d3gantt.js 中有一个 javascript 文件,其中包含gantt = function().

gantt = function() {
    //Does lots of stuff
    return gantt;
};

I then have a definition file called d3gannt.d.ts in the same folder which looks like this:

然后我在同一个文件夹中有一个名为 d3gannt.d.ts 的定义文件,如下所示:

declare module 'd3Gantt' {
    export module d3Gantt {
        export function gantt(): any;
    }
}

And then i reference it in my component as import * as d3Gantt from '../app/js/d3gantt';

然后我在我的组件中引用它作为 import * as d3Gantt from '../app/js/d3gantt';

However, i get the error message stating File 'c:/Projects/RepackLog/RepackLog/RepackLog/app/js/d3gantt.d.ts' is not a module

但是,我收到错误消息说明 File 'c:/Projects/RepackLog/RepackLog/RepackLog/app/js/d3gantt.d.ts' is not a module

Am i missing something that is needed for this to pick up my files properly?

我是否错过了正确获取我的文件所需的东西?

Thanks,

谢谢,

采纳答案by Priyal85

As @FabioAntunes mentioned in one of the comments above, you just have to export gantton your d3gantt.js file itself, in-line. The exact syntax would be

正如@FabioAntunes 在上述评论之一中提到的,您只需在线导出ganttd3gantt.js 文件本身。确切的语法是

export gantt = function() {
    //Does lots of stuff
    return gantt;
};

No need to declare it anywhere else. For further note on exporting modules please refer this post (Typescript es6 import module "File is not a module error").

无需在其他任何地方声明。有关导出模块的进一步说明,请参阅这篇文章(Typescript es6 导入模块“文件不是模块错误”)。

回答by ranakrunal9

Declare module as below :

声明模块如下:

declare module 'd3Gantt' {
  export function gantt(): any;    
}

Then import module as : import * from 'path to your module file'or import * as d3 from 'path to your module file'

然后将模块导入为:import * from 'path to your module file'import * as d3 from 'path to your module file'