Intellisense用于模块别名包 [英] Intellisense for module-alias Package

查看:129
本文介绍了Intellisense用于模块别名包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此npm模块(module-alias) 在我的Node项目中 https://www.npmjs.com/package/module-alias

I am using this npm module (module-alias) https://www.npmjs.com/package/module-alias in my Node project.

要使用此软件包,您必须在package.json文件中设置路径别名.

To make use of this package you have to set path aliases in the package.json file.

但是,使用此软件包有一个缺点,即智能感知不再起作用.

However, using this package comes with the disadvantage, that intellisense doesn't work anymore.

我的问题是如何使用这些路径别名启用智能感知?

推荐答案

问题是您没有在短绒棉的任何地方注册这些别名.我通常会建议在这里使用ESLint(即使您使用TypeScript,因为TSLint将会因为支持ESLint而停止使用).我的示例还将包括TypeScript结尾.如果您只想使其仅适用于JavaScript,则可以跳过eslint中的.ts .tsx扩展名.)因此,要进行智能操作,请在

The problem is that you did not register those aliases anywhere with your linter. I would gerenally suggest to use ESLint here (even if you use TypeScript as TSLint will be discontinued in favour of ESLint). My examples will include the TypeScript endings as well. If you definately want to make it work for JavaScript only you can skip the .ts .tsx extentensions in the eslint) So to make intellisense work do this in

.eslintrc.js

settings: {
  "import/resolver": {
    alias : {
      map: [
        ["@","./src"]
      ],
      extensions: [".js", ".jsx", ".ts", ".tsx"],
    },
  }
},

请注意,在这种情况下,您需要导入ESLint的插件 .如果尚未安装,请安装它.

Note that in this case you will need the import Plugin for ESLint. If you don't already have it install it.

如果您使用的是TypeScript,则还必须使编译器知道该别名.因此,将其添加到您的

If you are using TypeScript you will also have to make that alias known to your compiler. So add this to your

tsconfig.json

"compilerOptions": {
  "baseUrl": ".",
  "paths": [
    "@/*" : [
      "src/*"
    ]
  ]
}

这篇关于Intellisense用于模块别名包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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