我使用https://github.com/angular/quickstart如何在角度2 tapp中加載node-java庫?
npm install
npm install java --save
node node_modules/java/postInstall.js
npm install @types/java --save-dev
的package.json
"dependencies": {
...
"java": "^0.8.0",
...
},
"devDependencies": {
...
"@types/java": "^0.7.31",
...
},
我這是怎麼加入systemjs.config.js庫
map: {
...
'java': 'npm:java/lib/nodeJavaBridge.js'
},
當我運行NPM啓動輸出是
終端:
304 GET /java/lib/nodeJavaBridge.js
304 GET /java/build/jvm_dll_path.json
404 GET /lodash
404 GET /async
404 GET /path
404 GET /fs
Chrome瀏覽器開發工具控制檯:
http://localhost:3000/lodash Failed to load resource: the server responded with a status of 404 (Not Found)
ZoneAwareError__zone_symbol__error: Error: (SystemJS) XHR error (404 Not Found) loading http://localhost:3000/lodash
Error: XHR error (404 Not Found) loading http://localhost:3000/lodash
...
Error loading http://localhost:3000/lodash as "lodash" from http://localhost:3000/node_modules/java/lib/nodeJavaBridge.js
...
http://localhost:3000/async Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:3000/path Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:3000/fs Failed to load resource: the server responded with a status of 404 (Not Found)
測試,我可以加載節點的Java和運行Java代碼的代碼是一樣的東西:
import { Component, OnInit} from '@angular/core';
import * as java from 'java';
@Component({
selector: 'my-app',
template: `<h1>Hello {{name}}</h1>`,
})
export class AppComponent implements OnInit {
name = 'Angular';
ngOnInit() {
var sys = java.import('java.lang.System');
sys.out.printlnSync('Hello from java :)'); // outputs to terminal
var list1 = java.newInstanceSync("java.util.ArrayList");
console.log(list1.sizeSync()); // 0
list1.addSync('item1');
console.log(list1.sizeSync()); // 1
}
}
但代碼永遠不會因缺少圖書館而被稱爲。
的node_modules/JAVA/LIB/nodeJavaBridge.js包含這些行:
var _ = require('lodash');
var async = require('async');
var path = require('path');
var fs = require('fs');
我怎麼能正確加載節點Java庫在角2快速啓動應用程序?
角度不是節點,它運行在瀏覽器。它可能包括節點以方便啓動開發服務器,但它不能在節點環境中運行。 – Claies
你能否提供一個加載庫的解決方案?還是你說它不能工作,因爲節點FS依賴? – cosma
我一定在說它不是一個帶有角度的兼容庫,由於各種原因,其中最重要的是'fs'依賴。 'fs'是'filesystem'的簡稱,而瀏覽器根本無法訪問文件系統,因此也沒有角度。 – Claies