从字符串变量导入模块-JavaScript

时间:2019-07-20 02:59:21

标签: javascript angular typescript webpack systemjs

我需要从内存变量中导入JavaScript模块。 我知道可以使用 SystemJS Webpack 来完成。

但是我在任何地方都找不到合适的工作示例或文档。这些文档大多谈论动态导入.js文件。

基本上我需要像下面那样导入模块

let moduleData = "export function doSomething(string) { return string + '-done'; };"
//Need code to import that moduleData into memory

如果任何人都可以指向很棒的文档

4 个答案:

答案 0 :(得分:2)

您可以动态创建组件和模块。但不是来自字符串。这是一个示例:

name = "Dynamic Module on Fly";
const tmpCmp = Component({
  template:
    '<span (click)="doSomething()" >generated on the fly: {{name}}</span>'
})(
  class {
    doSomething(string) {
      console.log("log from function call");
      return string + "-done";
    }
  }
);
const tmpModule = NgModule({ declarations: [tmpCmp] })(class {});

this._compiler.compileModuleAndAllComponentsAsync(tmpModule).then(factories => {
  const f = factories.componentFactories[0];
  const cmpRef = this.container.createComponent(f);
  cmpRef.instance.name = "dynamic";
});

这里是stackblitz

答案 1 :(得分:2)

import语法中有一些局限性,如果不使用外部库就很难做到这一点。

我能得到的最接近的是使用Dynamic Import语法。下面是一个示例:

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<script>
    var moduleData = "export function hello() { alert('hello'); };";
    var b64moduleData = "data:text/javascript;base64," + btoa(moduleData);

</script>
<script type="module">

    async function doimport() {
      const module = await import(b64moduleData);
      module.hello();
    }

    doimport();

</script>

</body>
</html>

但是,您会注意到,这对导入代码的构造方式有一些限制,可能与您的需求不完全匹配。 最简单的解决方案可能是将模块的代码发送到服务器上,以使其生成临时脚本,然后使用更常规的语法将其导入。

答案 2 :(得分:0)

使用nodejs标志-实验模块以使用from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC url="http://example_url.com" driver_path="/driver/chromedriver.exe" driver = webdriver.Chrome(driver_path) driver.get (url) numberOfRows = len(driver.find_elements_by_xpath("//*[@id='someID']/table/tbody//tr")) for iRow in range(numberOfRows): # wait until the row is present (you need this when you are coming back to the row containing table currentRow = WebDriverWait(driver,30).until(EC.presence_of_element_located((By.XPATH,"(//*[@id='someID']/table/tbody//tr)[str(" + (iRow+1) + ")]"))) # if you want to access the link in the row linkInCurrentRow = currentRow.find_elements_by_xpath(".//a[@attribute='attribute_value']") # click on the link or you can perform desired operation linkInCurrentRow.click() #write the logic below to navigate to the table containing page driver.back()

import ... from ...;
index.mjs
node --experimental-modules index.mjs


进一步阅读here

工作方式:

  1. 我首先使用import fs from 'fs'; let fileContents = "export function doSomething(string) { return string + '-done'; };" let tempFileName = './.__temporaryFile.mjs'; fs.writeFileSync(tempFileName, fileContents); console.log('Temporary mjs file was created!'); import(tempFileName) .then((loadedModule) => loadedModule.doSomething('It Works!')) .then(console.log)
  2. 创建文件
  3. 然后我使用 import 方法的承诺来加载模块和
  4. 通过 pipe doSomething 方法进行调用!
  5. 然后记录doSomething的结果。

积分https://stackoverflow.com/a/45854500/3554534https://v8.dev/features/dynamic-import,@ Louis

答案 3 :(得分:-1)

Set-PrintConfiguration
相关问题