【问题标题】:Typescript 'is not a module' error打字稿'不是模块'错误
【发布时间】:2017-03-11 04:30:07
【问题描述】:

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

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

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

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

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

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

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

我是否缺少正确提取文件所需的东西?

谢谢,

【问题讨论】:

  • 您需要在您的 **app/js/d3gantt.js 上导出 export gantt; **
  • 好的,我把它放在哪里,语法是什么?我把我的 js 文件放在有问题的地方,删除了函数的内容,这样它们就不会妨碍

标签: javascript angular typescript


【解决方案1】:

正如上述 cmets 之一中提到的 @FabioAntunes,您只需在您的 d3gantt.js 文件本身上直接导出 gantt。 确切的语法是

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

无需在其他任何地方声明它。 有关导出模块的更多说明,请参阅此帖子 (Typescript es6 import module "File is not a module error")。

【讨论】:

    【解决方案2】:

    声明模块如下:

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

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

    【讨论】:

    • 把文件名改成d3gantt.ts再检查一遍
    • javascript 文件?
    • 您正在使用import * as d3Gantt from '../app/js/d3gantt' 导入,并且您说app/js/d3ganttapp/js/d3gantt.d.ts,因此将其名称更改为d3gantt.ts
    • 不,它没有用。我是否缺少能够将 javascript 文件映射到打字稿的东西
    • 检查stackoverflow.com/questions/40259765/…也可能对你有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-03
    • 1970-01-01
    • 1970-01-01
    • 2017-11-05
    相关资源
    最近更新 更多