简体   繁体   English

将jspm与TypeScript一起使用

[英]Using jspm with TypeScript

Is it possible to combine jspm with TypeScript and the TypeScript module system? 是否可以将jspm与TypeScript和TypeScript模块系统结合使用?

I couldn't find documentation or samples of this. 我找不到文档或样本。

Update - v1.5 更新 - v1.5

SystemJS has been added as a module kind for TypeScript, so you should be able to use SystemJS out of the box and compile with the module flag: SystemJS已添加为TypeScript的模块类型,因此您应该可以使用SystemJS开箱即用并使用模块标志进行编译:

tsc --module system app.ts

This was added alongside the ES6 module importing syntax, so you should be able to use that style and have it compiled to what you need. 这是与ES6模块导入语法一起添加的,因此您应该能够使用该样式并将其编译为您需要的样式。

import * as $ from "jquery";

Original Answer 原始答案

If you are using the SystemJS syntax, you can declare the parts you need like this: 如果您使用的是SystemJS语法,则可以声明所需的部分,如下所示:

systemjs.d.ts systemjs.d.ts

interface System {
    then: (cb: Function) => void;
}

interface SystemStatic {
    import: (name: string) => System;
}

declare var System: SystemStatic;

export = System;

You should then be able to use it like this: 然后你应该能够像这样使用它:

/// <reference path="jquery.d.ts" />

import System = require('systemjs');

System.import('jquery').then(($: JQueryStatic) => {
    $('#id').html('Hello world');
});

Update Feb. 2016 : As requested by Abhishek Prakash, I've setup an example repository on GitHub . 2016年2月更新 :根据Abhishek Prakash的要求,我在GitHub上设置了一个示例存储库 Feel free to play with the sources and see how the typescript and jspm workflow works. 随意使用源代码,看看typescript和jspm工作流程的工作原理。


As I wanted to try out TypeScript (referencing version 1.5 here) to work together with AngularJS and jspm, I couldn't find any solution, but stumbled over this question and Steve's answer, but I still couldn't make it working with that information. 因为我想尝试使用TypeScript(这里引用版本1.5)与AngularJS和jspm一起工作,我找不到任何解决方案,但偶然发现了这个问题和史蒂夫的回答,但我还是无法使用它来处理这些信息。 So I tried a lot of things on my own and finally made it working. 所以我自己尝试了很多东西,最后让它发挥作用。 So here is some example on how to use TypeScript with jspm in an AngularJS environment: 所以这里是一个关于如何在AngularJS环境中使用带有jspm的TypeScript的示例:

At first, install AngularJS with jspm with 首先,使用jspm安装AngularJS

jspm install angular

Then install the DefinitelyTyped definition files with tsd or by hand. 然后使用tsd或手动安装DefinitelyTyped定义文件。 Now you can import AngularJS with the ES6 syntax: 现在您可以使用ES6语法导入AngularJS:

/// <reference path="typings/angularjs/angular.d.ts" />

import * as angular from 'angular';

A quick note, as this got me crazy at the first tries: You will need both, a correct path to angular and the type definitions! 快速说明,因为这让我在第一次尝试时发疯:你需要两者,一个正确的角度路径类型定义! If one of both is missing (for me, I didn't include the reference line), the tsc will throw 如果两个中的一个丢失(对我来说,我没有包括参考线),tsc将抛出

error TS2307: Cannot find module 'angular'. 错误TS2307:找不到模块'angular'。

That is hard to debug, as I always thought, it couldn't find the module, but I just missed the definition reference. 这很难调试,正如我一直认为的那样,它无法找到模块,但我错过了定义参考。 So just make sure you always have the correct definitions present for everything you're trying to import! 因此,请确保始终为您要导入的所有内容提供正确的定义!

Then compile your TypeScript using tsc --module system app.ts , whereas the value for module may be commonjs, amd, system or umd. 然后使用tsc --module system app.ts编译TypeScript,而module的值可以是commonjs,amd,system或umd。 Please choose the module system you want to use in your environment! 请选择您要在您的环境中使用的模块系统! There is no need to use system for SystemJS. 没有必要使用SystemJS系统。 The TypeScript compiler will only wrap the specific selected module "template" around your code (or better said create a module of the specified module type), so it can be loaded with SystemJS later (when using --module system ). TypeScript编译器只会将特定的选定模块“模板”包装在您的代码周围(或者更好地表示创建指定模块类型的模块),因此可以在以后加载SystemJS(使用--module system )。

In my case, for a browser AngularJS environment I selected amd (using system did not work, as the es6-module-loader from SystemJS couldn't load the files that way... I don't know why yet!). 在我的情况下,对于浏览器AngularJS环境我选择了amd(使用system不起作用,因为来自SystemJS的es6-module-loader无法加载文件......我不知道为什么!)。

Using the --module amd switch, I could easily import the compiled file with SystemJS inside my page's HTML code using: 使用--module amd开关,我可以使用以下命令轻松地在我的页面的HTML代码中使用SystemJS导入已编译的文件:

<script src="jspm_packages/system.js"></script>
<script src="config.js"></script>
<script>
    System.import('src/app');
</script>

Hope this will help someone that is left alone in the documentation, like I was. 希望这会帮助那些独自留在文档中的人,就像我一样。

I've tried the current solutions posted on the official JSPM site and some git repos hinted in this post to no avail. 我已经尝试了官方JSPM网站上发布的当前解决方案,并且在这篇文章中暗示的一些git repos无济于事。 The reason is that handling of typescript enums files with the current TS implementation. 原因是使用当前的TS实现处理typescript枚举文件。 I suggest that you first transpile the application with the TS transpiler directly and then run this content through the JSPM build tools. 我建议您首先直接使用TS转换器来转换应用程序,然后通过JSPM构建工具运行此内容。

http://www.itsgettinghotamerica.com/2016/07/making-javascript-build-process-work.html http://www.itsgettinghotamerica.com/2016/07/making-javascript-build-process-work.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM