简体   繁体   English

Typescript baseUrl 在 React Typescript 项目中不起作用

[英]Typescript baseUrl not working in React Typescript project

I defined the baseUrl in my tsconfig.json file:我在tsconfig.json文件中定义了baseUrl

"baseUrl": "src",

In .eslintrc.js I have:.eslintrc.js我有:

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

Now in eg index.tsx I can import my components like import Layout from 'components/layout';现在在例如index.tsx中,我可以导入我的组件,例如import Layout from 'components/layout';

When I run gatsby develop I get some errors like:当我运行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

What's missing here, why it isn't finding my components?这里缺少什么,为什么找不到我的组件?

You have to add import/resolver settings in your .eslintrc.js so Webpack can identify imports of your components (I'm supposing that they have .tsx extension)您必须在.eslintrc.js中添加import/resolver设置,以便 Webpack 可以识别组件的imports (我假设它们具有.tsx扩展名)

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

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

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