簡體   English   中英

電子-Javascript(ES6)-導入遠程類

[英]Electron - Javascript (ES6) - import remote Class

編輯:
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')
      }));
    } );

它似乎無錯誤調整,但我不知道如何使用它...
var someVM = new SomeViewModel()例如不起作用(不驚訝...)。

這是SomeViewModel:

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

該類被混為一談,成為:

(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

這是個好方法嗎? (我知道安全性,這只是怎么做的)

在這種情況下,我將下載文件並將其存儲在磁盤上,一旦完成,則需要該文件。

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');
        });
    });
});

暫無
暫無

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

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