【问题标题】:Is there a way to import .html file from JavaScript?有没有办法从 JavaScript 导入 .html 文件?
【发布时间】:2021-10-23 21:22:45
【问题描述】:

我的 Cloudflare Worker 项目中有以下导入语句,用于将 html 文件作为字符串常量导入:

import userAgentJsch from './userAgentJsch';
import { parse } from 'cookie';
import test from '.test.html';

失败并出现错误:

找不到模块:错误:无法解析 '.test.html' in

我正在阅读关于需要配置我的 webpack.config.js 文件的信息,但我没有这样的文件。

你能帮忙吗?我只是想让我的html文件与js文件分开,但我找不到我能理解的解决方案。

【问题讨论】:

  • fs.readFile 有什么问题?如果您不了解基本的 node.js 内容,那么在 cloudflare 上构建将会超出您的想象:文档会期望您知道您不知道的事情,并且只会让您感到困惑。先去做一些基本的节点教程。在你学会跑步之前必须先学会走路,你知道的。
  • 我确实知道,只是没有任何效果:D。感谢您的评论。

标签: javascript html node.js webpack cloudflare


【解决方案1】:

视图文件夹结构用于渲染模板毫无疑问

在 nodejs 中读取文件使用这个:

const fs = require("fs");
const files = fs.readFileSync("new.html");

导入

var htmlContent = require('path/to/html/file.html');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-17
    • 2022-10-07
    • 2012-01-23
    • 2015-08-24
    • 2017-08-01
    • 2020-09-23
    • 1970-01-01
    相关资源
    最近更新 更多