简体   繁体   English

通过打字稿中的System.js导入RxJS

[英]Importing RxJS through System.js in typescript

I'm trying to import RxJS 5 into a typescript app using System.js as the module type. 我正在尝试使用System.js作为模块类型将RxJS 5导入到打字稿应用程序中。 It's almost there, but when I do 快到了,但是当我这样做时

import * as Rx from "path/to/rxjs/Rx";

the Rx object doesn't contain the module contents of Rx directly, but has a property named 'default' that DOES contain the module contents: Rx对象不直接包含Rx的模块内容,而是具有一个名为“ default”的属性,该属性确实包含模块的内容:

Rx.Observable.of(1,2,3) ... //does not work, Rx.Observable is undefined
Rx.default.Observable.of(1,2,3) ... //DOES work, and contains the module contents as expected

My tsconfig looks like this: 我的tsconfig看起来像这样:

{
    "compilerOptions": {
        "module": "system",
        "allowSyntheticDefaultImports" : true,
        "allowJs" : true,
        "noImplicitAny": true,
        "removeComments": false,
        "preserveConstEnums": true,
        "baseUrl" : "./",
        "sourceMap": true,
        "target": "es6",
        "lib" : ["dom","es6","dom.iterable","scripthost","es2015","es2016"],
        "strict": true,
        "forceConsistentCasingInFileNames": true,
        "outFile" : "./js/out.js"
    },
    "files": [
      "main.ts"
    ]
}

And my invocation of system.js looks like this: 我对system.js的调用如下所示:

System.config({
        "baseURL" : "/",
        packages: {
            '../node_modules/rxjs': {
              defaultExtension: 'js',
            }
          }
      });
      System.import("./js/out.js")
        .then(() => {
          console.log("js loaded");
          System.import("main");
        })
        .catch((err)=> {
          console.error("Error: " + err);
        });

I suspect that this is a configuration variable I need to pass to system.js, but the documentation on cross-module system imports isn't the most helpful to a newbie. 我怀疑这是我需要传递给system.js的配置变量,但是有关跨模块系统导入的文档对新手并不是最有用。 I've already tried setting the package type of ../node_modules/rxjs to 'cjs' and it has the same result. 我已经尝试将../node_modules/rxjs的包类型设置为'cjs',并且结果相同。

As an aside: 5 different module systems (AT LEAST!) for Javascript is sorta crazy. 顺便说一句:5种不同的Java模块系统(至少)! And I thought the Java Jigsaw brouhaha was bad. 而且我认为Java拼图浏览器不好。

You are importing all exports of path/to/rxjs/Rx in to a new object, Rx . 您正在将path/to/rxjs/Rx所有导出导入到新对象Rx

import * as Rx from "path/to/rxjs/Rx";

To instead import the module's default export, use this: 要改为导入模块的默认导出,请使用以下命令:

import Rx from "path/to/rxjs/Rx";

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

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