繁体   English   中英

在tsconfig.json中指定路径,而不使用相对路径:src / app / app.module.ts中的错误:错误TS2307:找不到模块“ @…”

[英]Specify path in tsconfig.json instead of using relative path : ERROR in src/app/app.module.ts: error TS2307: Cannot find module '@…'

我正在尝试使用tsconfig.jsonpath属性来解析组件的相对路径导入。 但是,它给出了以下错误。 不确定,为什么会出现此错误。 是否可能由于src/components文件夹不在src/app文件夹内?

实际错误:

ERROR in src/app/app.module.ts(12,33): error TS2307: Cannot find module '@advent/components'.

1)这是我的文件夹结构:

src
  - app 
      - app.module.ts
      - app.component.ts
      - app.component.html
      - app.component.css
  - components
      - modals
          - modals.component.ts
          - modals.component.html
          - modals.component.css 
      - index.ts
tsconfig.json

2)tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./src",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "es2015",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ],
    "paths": {
      "@advent/components/*": [
        "components/*"
      ]
    }
  }
}

3)index.ts

export * from './modals/modals.component';

4)modals.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'modals',
  templateUrl: './modals.component.html',
  styleUrls: ['./modals.component.scss']
})
export class ModalsComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}

5)app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { NgModule } from '@angular/core';

// Importing Components
import { AppComponent } from './app.component';
import { ModalsComponent } from '@advent/components';


@NgModule({
  declarations: [
    AppComponent,
    ModalsComponent
  ],
  imports: [
    BrowserModule,
    BrowserAnimationsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

更新tsconfig.json中的行

  1. 如果tsconfig.json文件位于src文件夹中,并且baseUrl = “。”
"@advent/components/*": [ "./components/*" ]
  1. 如果tsconfig.json文件位于src文件夹之外,并且baseUrl = “。
"@advent/components/*": [ "./src/components/*" ]
  1. 如果tsconfig.json文件位于src文件夹之外,并且baseUrl = “ ./src”
"@advent/components/*": [ "./components/*" ]

暂无
暂无

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

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