【问题标题】:Typescript baseUrl not working in React Typescript projectTypescript baseUrl 在 React Typescript 项目中不起作用
【发布时间】:2021-04-08 22:38:03
【问题描述】:

我在tsconfig.json 文件中定义了baseUrl

"baseUrl": "src",

.eslintrc.js 我有:

parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaFeatures: {
      jsx: true,
    },
    ecmaVersion: 2018,
    sourceType: 'module',
    project: './tsconfig.json',
  },

现在在例如index.tsx 我可以导入我的组件,比如import Layout from 'components/layout';

当我运行 gatsby develop 时,我收到一些错误,例如:

If you're trying to use a package make sure that 'components/layout' is installed. If you're trying to use a local file make sure that the path is correct.
error undefined failed

这里缺少什么,为什么找不到我的组件?

【问题讨论】:

    标签: reactjs typescript eslint-config-airbnb


    【解决方案1】:

    你必须在你的.eslintrc.js 中添加import/resolver 设置,这样Webpack 才能识别你的组件的imports(我假设它们有.tsx 扩展名)

    settings: {
        'import/resolver': {
          node: {
            paths: [
              'src'
            ],
            extensions: [
              '.ts',
              '.tsx'
            ]
          }
        }
      },
    

    【讨论】:

    • 谢谢,我仍然收到错误消息。我是否还必须添加 nom 包eslint-import-resolver-typescript
    • 其实,既然你用的是Gatsby,你可以用this这样的插件。我认为配置起来会更容易
    • 在查看其他插件之前,是否必须安装eslint-import-resolver-typescript 才能在.eslintrc.js 中使用上述设置import/resolver
    • 很遗憾我没用!
    猜你喜欢
    • 1970-01-01
    • 2021-04-19
    • 2021-11-11
    • 2019-10-26
    • 2021-05-22
    • 1970-01-01
    • 2019-03-07
    • 2019-08-05
    • 1970-01-01
    相关资源
    最近更新 更多