node.js - 如何使用 Typescript Express 应用程序渲染 Lit Element Web 组件?

标签 node.js typescript express tsc lit-element

我创建了一个 Typescript Express 服务器:

src/server.ts

    import express from "express";

    import { HomeController } from "./controllers";

    const app: express.Application = express();
    const port: number = ((process.env.PORT as any) as number) || 3000;

    app.use(express.static("static"));

    app.use("/", HomeController);

    app.listen(port, () => {
      // tslint:disable-next-line:no-console
      console.log(`Listening at http://localhost:${port}/`);
    });

src/controllers/index.ts

    import { Request, Response, Router } from "express";

    const router: Router = Router();

    router.get("/", (req: Request, res: Response) => {
      res.send("Hello World");
    });

    export const HomeController: Router = router;

结构

    ├───build
    │   ├───common
    │   ├───components
    │   └───controllers
    ├───src
    │   ├───common
    │   ├───components
    │   └───controllers
    └───static
        └───images

我尝试过托管静态文件。前任。索引.html。通过 res.send('index.html'); 文件已呈现,但我无法使用脚本标记导入元素。由于返回的错误是Exports is not Defined

src/components/card.ts

    import { html, LitElement } from "lit-element";

    class Card extends LitElement {
      protected render() {
        return html`
          <img src="../../static/images/AS.png" />
        `;
      }
    }

    declare global {
      interface HTMLElementTagNameMap {
        "card-element": Card;
      }
    }

    customElements.define("card-element", Card);

我正在使用 TSC 来构建我的应用程序。我手动将静态文件夹复制到构建文件夹中以供使用。我不确定是否有一种自动方法可以在构建时复制此文件夹。

我的编译器是否做错了什么,可能会给我错误导出未定义研究表明它与 CommonJs 有关,但我尝试安装,结果没有改变

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "lib": ["es6", "dom"],
    "outDir": "./build",
    "strict": true,
    "moduleResolution": "node",
    "typeRoots": ["./modules"],
    "esModuleInterop": true,
    "skipLibCheck": true
  }
}

最佳答案

我通过使用 webpack 捆绑我的代码解决了我的问题。这允许我创建一个 Index.html 导入我的bundle.js 文件

关于node.js - 如何使用 Typescript Express 应用程序渲染 Lit Element Web 组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56658421/

相关文章:

json - Typescript JSON反序列化以接口(interface)缺失的方法

javascript - 依赖注入(inject)在 Angular 2 入门应用程序中不起作用

angular - 如何获取 Angular ng-bootstrap 断点

javascript - 类型错误 : Cannot read property 'handle' of undefined --- if (fn. 句柄 && fn.set) mount_app = fn

node.js - 是否可以禁用/删除 ExpressJS 中特定路由的中间件?

javascript - 是否可以单击具有动态内容的页面上的每个元素?

Node.js 测试失败 - 找不到指定的过程,contextify.node?

javascript - 连接集合中所有元素的数组[MongoDB]

node.js - 我可以只使用redis和 Node 吗?

node.js - nodejs Sequelize orm model.validation 不是函数