【问题标题】:Can I import a variable from the front end script?我可以从前端脚本导入变量吗?
【发布时间】:2022-01-13 16:46:43
【问题描述】:

我正在尝试将数据数组从一个 HTML 文件传送到另一个 HTML 文件?这可能吗?如果可以,我该怎么做?也许使用

这是一个例子:

第一个 HTML 文件

<script>
    let tmp = <%- result %>;
    let a = ''
    for (const i in tmp){
        a += tmp[i]['PartitionKey'] + ' ' + tmp[i]['company_name']                
    }
    console.log(a);
    export{ a }
</script>

第二个 HTML 文件

<script>
    import { a } from './client.html'
    console.log(a);
</script>

【问题讨论】:

  • 你好 @noobdev123,你不能从 html 文件中导入,你可以让你的脚本成为一个模块,这样它们就支持导入和导出

标签: javascript html node.js dom frontend


【解决方案1】:

是的。但是你需要使用模块。例如,您有两个 .js 文件。 index.js 和 index2.js。

index.js 的代码

import { a } from './index2.js';

console.log({ a });

index2.js的代码

export const a = 10;

要在 js 中使用模块,您必须通过 &lt;script src="index.js" type="module"&gt;&lt;/script&gt; 将 index.js 连接到 .html 页面 它只适用于 http 服务器。要在本地运行 http 服务器,你可以使用这个包https://www.npmjs.com/package/http-server

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-02
    • 2016-09-22
    • 1970-01-01
    • 2013-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多