如果 TS 文件中的导入不是相关路径,则用于移动中断的 Nativescript 构建

Nativescript build for mobile breaks if imports in TS files are not relevant paths

当我尝试构建或 运行 我的 Web 应用程序的移动版本时,它是通过 Nativescript 使用 Angular 构建的,我收到多个编译器错误,例如:

src/app/search/search.module.ts(5,29): error TS2307: Cannot find module 'app/common/pipes/pipes.module'.

当我从相对路径导入文件时,不会发生这种情况。 例如,而不是: app/common/pipes/pipes.module 如果我这样做 ../../../common/pipes/pipes.module 我不会得到任何错误。

尽管修复看起来很容易,但我不想改变这一点,因为如果我们想要重构我们的应用程序或四处移动东西,相对路径会让我们的生活变得更加困难。

有人对此有解决方案吗?它仅在我使用 nativescript cli 而不是 angular cli 构建应用程序时发生。

您必须在 compilerOptions 内从您的 tsconfig.json

更新您的 paths 配置
   "paths": {
        "~/*": [
            "./src/*"
        ],
        "*": [
            "./node_modules/tns-core-modules/*",
            "./node_modules/*"
        ]
    }

现在我可以将 src 中的任何内容导入为 import ... from '~/app/...'