簡體   English   中英

如何在Angular 2 Tapp中加載node-java庫?

[英]How to load node-java library in an angular 2 tapp?

我正在使用https://github.com/angular/quickstart

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 start時,輸出為

終奌站:

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)

測試我可以加載node-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');

我如何正確在angular 2 quickstart應用程序中加載node-java庫?

SystemJS不知道如何加載那些節點java依賴項,您需要告訴它在systemjs.config.js文件中在哪里可以找到它們,例如:

map: {
  ...
  'java': 'npm:java/lib/nodeJavaBridge.js',
  'lodash': 'npm:lodash/lodash.js',
  'async': 'npm:...',
  'path': 'npm:...',
  'fs': 'npm:...',
},

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM