简体   繁体   English

使用intellij-idea从TypeScript中的根路径导入模块

[英]Import module from root path in TypeScript with intellij-idea

i know this topic is very much asked i try read it but still fail 我知道这个话题非常多,我尝试阅读但仍然失败

this is my tsconfig.json 这是我的tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es2017",
    "sourceMap": true,
    "declaration": true,
    "allowSyntheticDefaultImports": true,
    "baseUrl": ".",
    "rootDir": ".",
    "experimentalDecorators": true,
    "forceConsistentCasingInFileNames": true,
    "alwaysStrict": true,
    "locale": "zh-TW",
    "pretty": true,
    "paths": {
      "src/*": [
        "./src/*"
      ],
      "/root/src/*": [
        "./src/*"
      ]
    },
    "include": [
      "./src/**/*.ts"
    ],
    "exclude": [
      "node_modules"
    ],
    "newLine": "lf"
  }
}

this is my directory structure 这是我的目录结构

node_modules
src
  gw2taco
    index
test
  r

r.ts (all fail) r.ts(全部失败)

  • import 'src/gw2taco'; 导入'src / gw2taco'; console.log(7777); 的console.log(7777);
  • import '/src/gw2taco'; 导入'/ src / gw2taco'; console.log(7777); 的console.log(7777);

im use intellij-idea 我使用了智能想法

For the import 对于进口

Given this directory structure: 给定此目录结构:

node_modules
src
  gw2taco
    index.ts
test
  r.ts

One option for the import in r.ts is this: r.ts导入的一种选择是:

import "../src/gw2taco";

For the tsconfig.json 对于ts​​config.json

  • Delete the baseUrl , rootDir , and paths entries. 删除baseUrlrootDirpaths条目。
  • Put the include and exclude properties outside of the compilerOptions . includeexclude属性放在compilerOptions之外。
  • Add your test directory to the include . 将您的测试目录添加到include
  • Capitalize the newLine property to LF . newLine属性大写为LF

The file result looks like this: 文件结果如下所示:

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es2017",
    "sourceMap": true,
    "declaration": true,
    "allowSyntheticDefaultImports": true,
    "experimentalDecorators": true,
    "forceConsistentCasingInFileNames": true,
    "alwaysStrict": true,
    "locale": "zh-TW",
    "pretty": true,
    "newLine": "LF"
  },
  "include": [
    "./src/**/*.ts",
    "./test/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ]
}

All together in VSCode 一起在VSCode中

在此处输入图片说明

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

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