【问题标题】:Electron - Javascript (ES6) - import remote ClassElectron - Javascript (ES6) - 导入远程类
【发布时间】:2015-09-01 13:54:42
【问题描述】:

编辑:
02/09 -
导入代码似乎没问题(就像响应中的那个),但我的类代码不好(糟糕的 ES6 ?糟糕的转译?)

初始帖子:
我正在尝试从我的电子应用程序中导入一个远程类。

  • 我有一个静态服务器来托管我的 html/js 文件
  • 我想从该服务器导入一个类,以便在我的电子应用程序的主进程中使用它。

有可能吗?

我找到了一些解决方案,例如:

var vm = require('vm')
var concat = require('concat-stream');
require('http')
  .get(
    {
      host: 'localhost', 
      port: 8123, 
      path:"/dist/SomeViewModel.js" 
    }, 
    function(res) {
      res.setEncoding('utf8');
      res.pipe(concat({ encoding: 'string' }, function(remoteSrc) {
        vm.runInThisContext(remoteSrc, 'remote_modules/SomeViewModel.js')
      }));
    } );

似乎 tun 没有错误,但我不明白如何使用它...
var someVM = new SomeViewModel() 例如不起作用(并不惊讶...)。

这里是 SomeViewModel :

export default class SomeViewModel {
  constructor(options) {
    this.element1 = options.element1,
    this.element2 = options.element2
  };
}

该类被 babelized 并变为:

(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var SomeViewModel = function SomeViewModel(options) {
  _classCallCheck(this, SomeViewModel);

  this.element1 = options.element1, this.element2 = options.element2;
};

exports["default"] = SomeViewModel;
module.exports = exports["default"];

},{}]},{},[1]);

//# sourceMappingURL=SomeViewModel.js.map

这是一个好方法吗? (我知道安全,只是如何)

【问题讨论】:

  • vm.runInThisContext 需要什么?如果你的外部JS文件只是一个类,那么var SomeViewModel = require('./remote_modules/SomeViewModel.js')有什么问题?
  • 嗨,我收到 Cannot find module './remote_modules/SomeViewModel.js' 错误。我不明白./remote_modules 对应的是什么,因为没有创建路径。 (代码来自另一个 StackOverflow 帖子)我没有找到关于那个的任何其他信息
  • 嗯,当然您获得的̶e̶r̶r̶o̶r̶.̶错误在该节点找不到̶f̶i̶l̶e̶.̶投入实际位置的̶f̶i̶l̶e̶.̶编辑:我重读你的问题,现在明白了此文件来自某个外部服务器。你问题的第一块JS代码,是在主进程还是渲染器进程?
  • 该代码在主进程中。

标签: javascript ecmascript-6 electron


【解决方案1】:

在这种情况下,我会下载文件并将其存储在磁盘上,完成后,需要该文件。

var http = require('http'),
    fs = require('fs');

var file = fs.createWriteStream('./tmp/SomeViewModel.js');
http.get({
    // your options
}, function (res) {
    // set encoding, etc.
    res.pipe(file);
    file.on('finish', function() {
        file.close(function() {
            // do stuff
            // var SomeViewModel = require('./tmp/SomeViewModel.js');
        });
    });
});

【讨论】:

  • 文件已下载,需要运行。但是当我尝试实例化 SomeViewModel 时,我收到一条错误消息 TypeError: SomeViewModel is not a function。我之前已经遇到过这个错误,我认为这是我的课不好。也许它来自我的 babel/gulp/... 配置。我认为起始代码很好(您的最后一个代码表明问题不在于这里)。我关闭该问题并打开一个新问题。非常感谢你 ! PS:需要在运行该代码之前创建文件或通过异常创建文件。
  • 1) 您可以测试上面的代码,但使用不同的、非常简单的程序代替 SomeViewModel.js 中的 ES6 类。 2)至于之前需要创建文件,我相信你只需要创建tmp文件夹,如果需要,节点会创建文件。 3)如果此代码回答了您的问题,请接受作为答案。谢谢
  • 1) TypeError: xxx is not a function,在这种情况下似乎对应于ES6 constructor中的thisbabelized时有问题。 - 2)文件夹和文件需要在这里:-s
猜你喜欢
  • 2018-12-09
  • 2016-05-24
  • 2017-12-03
  • 1970-01-01
  • 2019-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-07
相关资源
最近更新 更多