[英]In Typescript how do I use a Javascript module when importing from Typeings
Using mgechev's angular2-seed , I'm trying to get to grips with Angular2 and Typescript for a new project, but have run into this problem. 使用mgechev的angular2-seed ,我正在尝试使用Angular2和Typescript来处理一个新项目,但是遇到了这个问题。
I want to use Numeral in a component, so I: 我想在组件中使用Numeral,所以我:
npm install numeral
npm install numeral
typings install dt~numeraljs --global --save
typings install dt~numeraljs --global --save
Numeral的输入 import { numeral } from '/typings/globals/numeraljs';
import { numeral } from '/typings/globals/numeraljs';
let num:Number = new Number(numeral().unformat(text));
let num:Number = new Number(numeral().unformat(text));
So far, so good. 到现在为止还挺好。 Everything seems to transpile ok.
一切似乎都很好。 Until I get to the browser, where I get in the console:
直到我进入浏览器,我进入控制台:
Error: XHR error (404 Not Found) loading http://localhost:5555/typings/globals/numeraljs.js(…)
What am I doing wrong here? 我在这做错了什么? Have I missed a step out to tell Typescript where the actual code is?
我是否错过了告诉Typescript实际代码的位置?
Typically what you want to do is, if the package is not a native Typescript module: 通常,您要做的是,如果包不是本机Typescript模块:
import * as numeral from 'numeral';
The typings folder is just for telling Typescript what the type definitions are, so it will use it for code highlighting and linting. typings文件夹仅用于告诉Typescript类型定义是什么,因此它将用于代码突出显示和linting。 The actual module you want to import sits in the
node_modules
folder and can be imported with its name. 要导入的实际模块位于
node_modules
文件夹中,可以使用其名称导入。
If it's still complaining about not finding the numeral
module you could add 如果仍然抱怨没有找到
numeral
模块,你可以添加
/// <reference path="typings/globals/numeraljs/numeraljs.d.ts"/>
or wherever the Typescript definition file is stored. 或存储Typescript定义文件的任何位置。
you need also to add a reference link to actual numeraljs.js file. 您还需要添加实际numeraljs.js文件的引用链接。 in your index.html page
在index.html页面中
say that you have index.html page as your main page, add the numeraljs.js to head 假设您有index.html页面作为主页面,请将numbersjs.js添加到head
<head>
<script src="myScriptLib/numeral/numeraljs.js"></script>
</head>
also you can use system.js to load all needed scripts 您也可以使用system.js加载所有需要的脚本
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.