简体   繁体   中英

React typescript - Cannot find module

Folder structure:

src
  - containers
    - Home.tsx

I want to lazy load, so I'm trying:

const Home = React.lazy(() => import('containers/Home'));

VS Code shows 'containers/Home' as incorrect and when hovering over it complains:

Cannot find module 'containers/Home' or its corresponding type declarations.

I have paths set in tsconfig as:

"baseUrl": "./src",
"paths": {
    "modules/*": [
        "src/modules/*"
    ],
    "containers/*": [
        "src/containers/*"
    ]
}

Why does it not find containers/Home when importing using React.lazy() ?

set paths in tsconfig this way

"baseUrl": "src",
"paths": {
    "@modules/*": [
        "src/modules/*"
    ],
    "@containers/*": [
        "src/containers/*"
    ]
}

and use it like this

const Home = React.lazy(() => import('@containers/Home'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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