【问题标题】:Typescript - Import modules that only extend globalsTypescript - 导入仅扩展全局变量的模块
【发布时间】:2015-10-01 11:55:55
【问题描述】:

在需要引导的 JS 模块中,我目前正在编写

define(["dep1", "dep2", "bootstrap"], function(dep1, dep2) { });

即:引导依赖项没有变量,因为该模块仅扩展全局 JQuery 对象而不导出任何内容。

如何在 Typescript 中实现相同的行为?我试过了

import dep1 = require("dep1");
import dep2 = require("dep2");
import bs = require("bootstrap");

但是生成的JS就像

define(["require", "exports", "dep1", "dep2"], function($, exports, dep1, dep2) { });

并且没有加载引导模块。

提前致谢

【问题讨论】:

    标签: module typescript external require amd


    【解决方案1】:

    解决方案(需要 Typescript 1.5+):

    之前

    import $ = require("jquery");
    

    添加

    ///<amd-dependency path="bootstrap"/>
    

    路径要么是文件的相对路径,要么是你在 require.config({ paths: ... }) 中定义的 require 路径 这是因为这个标签中的path属性的内容会按字面意思粘贴在生成的js文件的define语句中!

    【讨论】:

      【解决方案2】:

      试试这个:

      import = "bootstrap";
      

      【讨论】:

        【解决方案3】:

        发生这种情况是因为 TypeScript 编译器找不到名称为 bootstrap 的模块,这就是省略该模块的原因。

        尝试将bootstrap.d.ts 集成到您的项目中。或者声明自己的模块,名称为 bootstrap,然后导入。

        This example 表示未声明的模块已被省略

        更新

        刚刚发现bootstrap.d.ts,发现支持AMD模块,可以在import中使用

        另见

        1. Using d.ts

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-11-21
          • 2018-09-06
          • 2013-10-05
          • 2014-02-18
          • 1970-01-01
          相关资源
          最近更新 更多