简体   繁体   English

在Typescript中,如何从Typeings导入时使用Javascript模块

[英]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,所以我:

  1. Installed Numeral using npm install numeral 使用npm install numeral
  2. Installed the typing for Numeral using typings install dt~numeraljs --global --save 使用typings install dt~numeraljs --global --save Numeral的输入
  3. In my component added import { numeral } from '/typings/globals/numeraljs'; 在我的组件中添加了import { numeral } from '/typings/globals/numeraljs';
  4. Added the line of code: 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.

相关问题 如何在打字稿中使用 javascript 模块 - how to use a javascript module in typescript 如何从JavaScript代码模块上下文中使用FormData? - How do I use FormData from JavaScript Code Module context? 如何使用javascript中的setinterval显示导入任务的状态? - How do i use setinterval in javascript to display the status of the Importing task? 回复:如何将javascript AMD模块导入外部TypeScript模块? (使用module.exports =…时) - Re: How can I import a javascript AMD module into an external TypeScript module? (When using module.exports=…) 如何在不切换到 TypeScript 的情况下将 TypeScript 类型添加到 JavaScript 模块? - How do I add TypeScript types to a JavaScript module without switching to TypeScript? Deno:将JavaScript模块导入TypeScript会出错 - Deno: Importing a JavaScript module into TypeScript gives error 从 Javascript 迁移到 Typescript 时,如何确定数据类型? - When migrating from Javascript to Typescript, how do you determine datatypes? 如何在Typescript中的javascript中转换`arguments`的使用 - How do I convert use of `arguments` in javascript in Typescript 如何在 TypeScript 中使用“then”? - How do I use “then” with TypeScript? 从 TypeScript 调用 JavaScript function 时,如何覆盖推断类型? - When calling a JavaScript function from TypeScript, how do I override the inferred type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM