简体   繁体   English

如何在Angular 2 Tapp中加载node-java库?

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

I am using https://github.com/angular/quickstart 我正在使用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 的package.json

"dependencies": {
    ...
    "java": "^0.8.0",
    ...
},
"devDependencies": {
    ...
    "@types/java": "^0.7.31",
    ...
},

This is how I added the library in systemjs.config.js 这就是我在systemjs.config.js中添加库的方式

map: {
  ...
  'java': 'npm:java/lib/nodeJavaBridge.js'
},

When I run npm start the output is 当我运行npm start时,输出为

Terminal: 终奌站:

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 dev tools console: 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)

The code to test that I can load node-java and run java code is something like: 测试我可以加载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
    }
}

But the code never gets called because of the missing libraries. 但是由于缺少库,因此永远不会调用该代码。

The node_modules/java/lib/nodeJavaBridge.js contains these lines: node_modules / java / lib / nodeJavaBridge.js包含以下几行:

var _ = require('lodash');
var async = require('async');
var path = require('path');
var fs = require('fs');

How could I load the node-java library in the angular 2 quickstart app correctly ? 我如何正确在angular 2 quickstart应用程序中加载node-java库?

SystemJS doesn't know how to load those node-java dependencies, you need to tell it where to find them in your systemjs.config.js file, eg: 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