JavaScript:我可以';我什么也不进口

JavaScript:我可以';我什么也不进口,javascript,import,lit-html,Javascript,Import,Lit Html,如何在代码中导入lit html? 我有以下代码: js文件夹中的index.js文件: import { html } from "lit-html"; myDiv = html` <div> <a href="">Hello, Click Me!</a> </div> `; document.body.innerHTML += myDiv; 我得到了这样一个错误: Uncaught TypeError: Fai

如何在代码中导入lit html? 我有以下代码:

js
文件夹中的
index.js
文件:

import { html } from "lit-html";
myDiv = html`
    <div>
        <a href="">Hello, Click Me!</a>
    </div>
`;
document.body.innerHTML += myDiv;
我得到了这样一个错误:

Uncaught TypeError: Failed to resolve module specifier "lit-html". Relative references must start with either "/", "./", or "../".
你可以试试

import {render, html } from "https://unpkg.com/lit-html@0.7.1/lit-html.js"
HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>JS Tests</title>
</head>
<body>
    <script type="module" src="js/index.js"></script>
</body>
</html>

JS测试
JS

import {render, html } from "https://unpkg.com/lit-html@0.7.1/lit-html.js";
const myDiv = html`
  <div>
    <a href="">Hello, Click Me!</a>
  </div>
`;

render(myDiv,document.body);
从导入{render,html}”https://unpkg.com/lit-html@0.7.1/lit html.js”;
constmydiv=html`
`;
呈现(myDiv,document.body);

请注意,如果您在浏览器上使用模块(如上所述),每次导入都将进行网络调用。在将代码发送到浏览器之前,您可以使用一些模块绑定器(如webpack)构建一个JS文件(包含所有依赖项)。

可能会按照错误消息中的说明执行吗?使用
/
/
./
预先编写
lit html
。/为什么需要一个库来创建html?您可能还没有安装它。@Nicolas,因为我想在JavaScript文件中为html代码着色。
import {render, html } from "https://unpkg.com/lit-html@0.7.1/lit-html.js";
const myDiv = html`
  <div>
    <a href="">Hello, Click Me!</a>
  </div>
`;

render(myDiv,document.body);