Angular2-Webpack-Typescript-第三方库 [英] Angular2-Webpack-Typescript - 3rd party libraries

查看:64
本文介绍了Angular2-Webpack-Typescript-第三方库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个美丽的种子项目开始: https://github.com/AngularClass/angular2-webpack-starter

I've started with a beautiful seed project: https://github.com/AngularClass/angular2-webpack-starter

我一直坚持使用第三方模块.有人可以帮助/解释如何在此项目中正确添加/使用外部模块吗?

And I've stuck with using 3rd-party modules.Can someone help/explain how to properly add/use external modules to this project?

例如,我使用以下命令添加不可变":

For example, I'm adding 'immutable' using:

npm install immutable --save

在那之后,我试图在组件内部链接该模块:

After that, I'm trying to link that module inside a component:

import * as immutable from 'immutable';
import {List} from 'immutable';
import Immutable = require('immutable');

没有帮助,它总是返回TS2307:找不到模块"immutable".例如,对于"moment"也是如此.

Nothing helps, it always returns TS2307: Cannot find module 'immutable'. The same for 'moment' for example.

推荐答案

您需要将一个打字文件添加到项目中,该文件位于node_modules\immutable\dist\immutable.d.ts中.您需要在项目中引用它.它应该看起来像这样:

There is a typings file you need to add to your project that is located in node_modules\immutable\dist\immutable.d.ts. You need to reference that in your project. It should look something like this:

/// <reference path="node_modules/immutable/dist/immutable.d.ts" />
import * as immutable from 'immutable';
import {List} from 'immutable';
import Immutable = require('immutable');

暂时可以在 DefinitelyTyped 中进行键入.我建议使用 tsd 进行安装.

For moment there are typings available at DefinitelyTyped. I recommend using tsd to install them.

如果遇到您想使用的库而找不到任何类型的库,则可以看到

If you come across a library you would like to use that you can't find any typings for, you can see this question for an easy way to still use it. You just won't get any of the Typescript benefits.

这篇关于Angular2-Webpack-Typescript-第三方库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆