小编AMA*_*RMA的帖子

ts-node 无法编译 JSX 标签

我试图将 JSX 标签编译为 HTML,然后将它们刷新到 HTML 文件中,但不知何故ts-node无法编译以下代码段。

private index = (request: Request, response: Response) => {
    // const component = React.createElement(Index, {key: 1, name: "Aman Sharma"}) // this works
    const component = <Index key={1} name="Aman Sharma" /> // throws error
    const html = ReactDOMServer.renderToString(component)
    fs.writeFileSync('website/index.html', html);
    response.status(200).send();
}
Run Code Online (Sandbox Code Playgroud)

这是抛出的错误。

在此输入图像描述

我已经包含"jsx": "react"在我的tsconfig.json. 我的配置如下:

{
      "compilerOptions": {
      "target": "es6",
      "module": "commonjs",
      "lib": [
          "es2015",
          "dom"
      ],
      "moduleResolution": "node",
      "sourceMap": true,
      "esModuleInterop": true,
      "allowSyntheticDefaultImports": true,
      "outDir": "build",
      "jsx": …
Run Code Online (Sandbox Code Playgroud)

node.js express typescript reactjs

9
推荐指数
1
解决办法
1494
查看次数

标签 统计

express ×1

node.js ×1

reactjs ×1

typescript ×1