首页 > 解决方案 > 打字稿导入/导出给出“SyntaxError: Unexpected token 'export'”

问题描述

我有一个使用打字稿的 asp.net 核心项目。我正在使用这样的导入/导出语法:

import ConfigurationDocument from "./Configuration Document";
import  ServiceEndpoint  from "./Service - Endpoints";

export default class ConfigurationDocumentManager

该项目构建良好,但是当我尝试导航到使用这样链接的 ts 的页面时:

<script src="/js/Manager - Configuration Document.js" type="module"></script>

我收到错误:

SyntaxError: Cannot use import statement outside a module
SyntaxError: Unexpected token 'export'

文件命名约定是我工作场所的标准,所以我不知道这是否是问题所在。如果我将所有 ts 文件放在 wwwroot 中,删除 tsconfig 并删除所有导入/导出,项目构建并运行良好,所以我认为这不是问题。

我的 tsconfig 如下:

{
  "compilerOptions": {
    "noImplicitAny": false,
    "noEmitOnError": true,
    "removeComments": false,
    "sourceMap": true,
    "target": "ES2020",
    "lib": [ "ES2020", "DOM" ],
    "outDir": "wwwroot/js/",
    "module": "ES2020"
  },
  "exclude": [
    "node_modules",
    "wwwroot"
  ]
}

我确信这很明显,但我已经用谷歌搜索了好几天,并尝试了数十种解决方案,但没有任何效果。如果有人可以向我展示一个可以工作的 tsconfig 文件,那就太好了。

我的项目是这样的:

>root
    >wwwroot
    >TypeScript
    (c# files etc)

标签: typescript

解决方案


推荐阅读