简体   繁体   English

路径在tsconfig.json中无法正常工作

[英]paths is not working in tsconfig.json as expected

I'm trying to use jasmine-ts but always getting module not found error, so I decided to check that using tsc to figure out what is the problem, my folder structure is as follows: 我正在尝试使用jasmine-ts但总是出现找不到模块的错误,因此我决定检查是否使用tsc找出问题所在,我的文件夹结构如下:

\src\app\...
\src\tests\...

So I created tsconfig.json at ' tests ' folder level as follows 所以我在“ tests ”文件夹级别创建了tsconfig.json ,如下所示

{
    "compileOnSave": false,
    "compilerOptions": {
        "baseUrl": "../", 
        "paths": {
            "@app/*": [
                "app/*"
            ] 
        }
    }
}

in my *.test.ts file, I'm importing a module inside the app dir, and yet still getting module not found error. 在我的*.test.ts文件中,我正在应用程序目录中导入一个模块,但仍然出现模块未找到错误。

Cannot find module '@app/core/data/my-module 找不到模块'@ app / core / data / my-module

I tried the following command 我尝试了以下命令

tsc --traceResolution test1.test.ts

and it gives me 它给了我

test1.test.ts:2:23 - error TS2307: Cannot find module '@app/core/data/my-module'. 

2 import { Field } from '@app/core/data/my-module'
                         ~~~~~~~~~~~~~~~~~~~~~~~~~~

Try 尝试

"baseUrl": "src"

instead of 代替

"baseUrl": "../",  

in your tsconfig.json 在您的tsconfig.json中

Did you try like this?: 您尝试过这样吗?:

in tsconfig.json: 在tsconfig.json中:

{
  "compilerOptions": {
    "baseUrl": "src",
    "paths": {
      "@pages/*": ["app/pages/*"],
      "@environment": ["environments/environment"]
    }
  },
}

when importing: 导入时:

import { LoginComponent } from '@pages/login/login.component';

import { environment } from '@environment';

It is working for me.. 它正在为我工​​作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM