gpt4 book ai didi

javascript - 使用 TypeScript 的 RxJ : How generate Production files for Web?

转载 作者:行者123 更新时间:2023-12-03 06:03:30 28 4
gpt4 key购买 nike

我想使用 RxJs (5.0.0-Beta.6)TypeScript (1.8.10) 创建一个 JS 库>.

我的简单 TypeScript 文件正在编译。我有这个文件:

MyLib.ts:

/// <reference path="../../typings/globals/es6-shim/index.d.ts" />
import * as Rx from 'rxjs/Rx';
Rx.Observable.of('foo', 'bar');

tsconfig.json:

{
"compilerOptions": {
"module": "commonjs"
,"target": "es5"
,"sourceMap": true
},
"files": [
"src/MyLib.ts"
]
}

我使用gulp-typescript生成JS文件,它生成这个文件:

MyLib.js:

"use strict";
var Rx=require("rxjs/Rx");
Rx.Observable.of("foo","bar");

现在我需要将此文件保存为 HTML 格式。 RxJs 需要依赖项,因此我复制了这些依赖项:

  • RxJs 5.0.0 Beta 6 >> node_modules/rxjs/bundles/Rx.min.js

  • SystemJS 0.19.38 >> node_modules/systemjs/dist/system.js

  • RequireJS 2.3.2 >> node_modules/requirejs/require.js

这是我的 HTML:

<!DOCTYPE html>
<html lang="en">
<head>

<meta charset="UTF-8">
<title>MyLib Test</title>

<script src="vendor/require.js"></script>
<script src="vendor/system.js"></script>
<script src="vendor/Rx.min.js"></script>
<script src="MyLib.js"></script>

</head>
<body>

</body>
</html>

我的问题:

我在 Chrome 控制台中收到此错误:

未捕获错误:尚未为上下文加载模块名称“rxjs/Rx”:_。使用 require([])

我无法加载这个简单的 JS:用 RxJs 和 TypeScript 制作的 MyLib.js

我的问题是什么以及如何解决它?

最佳答案

您忘记配置模块加载器(并且由于某种原因包括其中两个(require 和 systemjs))。

你应该只留下一个,你的带有 systemjs 的 html 看起来有点类似于:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>MyLib Test</title>
<script src="vendor/system.js"></script>
<script>
System.config({
paths: {
"rxjs": 'vendor/rx/dist/rx.min'
}
});

System.defaultJSExtensions = true;

//This will bootstrap your app
System.import('myLib').catch(function(e)
{
console.error(e);
});;
</script>
</head>
<body>

</body>
</html>

希望这有帮助。

关于javascript - 使用 TypeScript 的 RxJ : How generate Production files for Web?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39644318/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com