tsconfig.json被Visual Studio代码忽略 [英] tsconfig.json ignored by Visual Studio Code

查看:60
本文介绍了tsconfig.json被Visual Studio代码忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目的根目录下有一个 tsconfig.json 文件:

I have a tsconfig.json file at the root of my project:

{
  "compilerOptions": {
    "target": "ES5",
    "module": "commonjs",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "sourceMap": true,
    "noEmitHelpers": true,
    "baseUrl": ".",
    "paths": {
      "services/*": ["./src/app/shared/services/*"]
    }
  },
  "compileOnSave": false,
  "buildOnSave": false,
  "awesomeTypescriptLoaderOptions": {
    "forkChecker": true,
    "useWebpackText": true
  }
}

我正在尝试定义路径,因此我们无需在项目中使用相对路径.

I am trying to define my paths so we don't need to use relative paths in the project.

例如:

我试图从'services/config'; import {ConfigService}; 正常工作,而不必键入:

I am trying to get import { ConfigService } from 'services/config'; to work, instead of having to type:

从'../../shared/services/config/'; {code>

import { ConfigService } from '../../shared/services/config/';

但是当我尝试这样做时,我在webpack中不断出现错误.

but I keep getting errors in webpack when I try this.

看起来只是在尝试查看/node_nodules 而忽略了 tsconfig.json 吗?我说的对吗?

Looks like it is only trying to look in the /node_nodules and ignoring the tsconfig.json? Am I right?

我真的不知道我在做什么错.谷歌搜索了两个多小时,然后变得疯狂了.

I really don't know what I'm doing wrong. Been googling for more than a couple hours and am going crazy.

任何帮助将不胜感激.

推荐答案

注意:实际上,您是对的,Webpack不了解tsconfig文件,而是尝试以自己的方式解析模块.为了与tsconfig一起使用,您需要一个TsConfigPathsPlugin插件.请在github上参阅此 issue .

Note: Actually, you were right, Webpack is unaware of the tsconfig file and tries to resolve the modules in its own way. In order to use it with tsconfig you would need a TsConfigPathsPlugin plugin. Please refer to this issue on github.

我认为您的路径属性设置不正确.您需要告诉打字稿这样的内容:

I think that you paths property is not setup correctly. You would need to tell typescript something like this:

对于与模式"services/"匹配的导入,请将以下路径附加到导入(假设您的根目录在src之前是一个)"/src/app/shared/"

For the imports that match pattern "services/" append the following path to the import (asumming that you root dir is one before src) "/src/app/shared/"

"paths": {
  "services/*": ["/src/app/shared/*"]
}

因此,当编译器进入"services/config"导入时,它将匹配该模式,然后创建此路径"root/src/app/shared/services/config"以尝试解决它.

So, when the compiler gets to your "services/config" import, it will match the pattern and then create this path "root/src/app/shared/services/config" to try and resolve it.

有关详细信息,请咨询打字稿模块解析文档.

Consult typescript module resolution documentation for more details.

这篇关于tsconfig.json被Visual Studio代码忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆