我试图使用webpack出色的类型脚本加载程序来捆绑我的项目,但是有几个模块没有将自己导出为模块,我遇到了麻烦。
一个例子是引导对话框npm包(我已经安装了引导开关和@type/引导开关包)。@type不导出模块,而只是声明名称空间,并添加到JQuery接口中。:https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/bootstrap-switch/index.d.ts
在我的app.ts中,我是这样使用它的:
import {OtherClass} from './otherclass'
$(document).ready(()=>{
let thisPage= new PageEntry ();
thisPage.init();
}
class PageEntry {
public init(): void {
$('.bss').bootstrapSwitch();
let x = new OtherClass();
x.doSomething();
}
}但是webpack的输出不包括bootstrapSwitch代码。我试着添加:import * as BootstrapSwitch from 'bootstrap-switch';,但是很明显,我在声明
node_modules/@types/bootstrap-switch/index.d.ts‘不是一个模块。
我如何确保引导开关被拉进来?
我正在使用引导开关作为一个例子,但我有大约5个包与同样的问题。
发布于 2017-05-12 22:57:52
如果一个文件没有导出,因此只为其副作用而导入(例如,它增强了jQuery),那么在ECMAScript中导入它的正确方法,以及通过扩展TypeScript导入它的正确方法如下
import 'bootstrap-switch';一个更完整的例子:
import $ from 'jquery';
import 'bootstrap-switch';
$("[name='my-checkbox']").bootstrapSwitch();发布于 2017-05-12 21:49:42
当然,import * as bs from 'bootstrap-switch';会出现错误,因为没有导出任何模块。
然而,以下两句话对我有效..。只是不确定这是最好的方法:
declare var require: any;
let bs = require('bootstrap-switch');
import {OtherClass} from './otherclass'希望这能帮上忙。如果有更好的方法告诉我。
https://stackoverflow.com/questions/43946857
复制相似问题