【问题标题】:Typescript paths not resolved打字稿路径未解析
【发布时间】:2020-09-21 21:31:56
【问题描述】:

我一直在尝试让路径解析在我的项目中工作,但我找不到让它工作的方法。 我的项目是一个使用电子 webpack 的电子项目。

我的文件夹结构如下:

我想为 ./src/main 和 ./src/renderer 注册 @main 和 @render 的路径 我的 tsconfig.json 目前看起来是这样的:

{
  "compilerOptions": {
    "target": "es2017",
    "module": "esnext",
    "moduleResolution": "node",
    "noUnusedLocals": true,
    "noImplicitReturns": true,
    "inlineSources": true,
    "inlineSourceMap": true,
    "experimentalDecorators": true,
    "newLine": "lf",
    "noEmitOnError": true,
    "jsx": "react",
    "esModuleInterop": true,
    "lib": ["es2015", "es2016", "DOM"],
    "typeRoots" : ["./node_modules/@types"], 
    "baseUrl": "./src",
    "paths": {
      "@main/*" : ["main/*"],
      "@render/*" : ["renderer/*"]
    },
   "allowSyntheticDefaultImports": true,
   "resolveJsonModule": true
  }
}

然而 VS Code 警告我,它找不到模块。该行如下所示:

import { Api } from '@render/api'

我该如何解决这个问题?

【问题讨论】:

  • 你试过重启IDE和TS服务吗?当您更改配置时,必须重新启动 TS 以检测新更改。
  • 嗯,不,我没有尝试将其关闭然后再打开。有效。谢谢,我不知道。
  • 太棒了!添加它作为答案。

标签: typescript webpack


【解决方案1】:

您需要重新启动 IDE 和 TS 服务。当您更改配置时,必须重新启动 TS 以检测新更改。

【讨论】:

    猜你喜欢
    • 2019-08-18
    • 2018-07-30
    • 2019-03-22
    • 1970-01-01
    • 2016-08-22
    • 2022-01-25
    • 1970-01-01
    • 2017-01-22
    • 2021-11-27
    相关资源
    最近更新 更多