2016-09-22 74 views
0

我倒是喜歡用RxJs(5.0.0-Beta.6)打字稿(1.8.10)創建JS庫帶TypeScript的RxJs:如何爲Web生成生產文件?

我的簡單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" 
    ] 
} 

I'm使用一飲而盡,打字稿生成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 /包/ 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>

我的問題:

I'm得到這個錯誤在Chrome控制檯:

未捕獲的錯誤:模塊名稱 「rxjs /接收」 一直沒有尚未加載上下文:_。使用require([])

我一直無法加載這個簡單的JS:MyLib.js使用RxJs和TypeScript。

我的問題是什麼,如何解決?

回答

1

你忘了配置你的模塊加載器(並由於某種原因包括其中兩個(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> 

希望這有助於。