简体   繁体   English

带有 React 的打字稿,找不到导入

[英]Typescript with React, import not found

i'm trying to import using modules, but i dont know what i'm doing wrong, can anyone help me?我正在尝试使用模块导入,但我不知道我做错了什么,有人可以帮助我吗?

the app is here:该应用程序在这里:

https://codesandbox.io/s/vigilant-sea-3q8v08 https://codesandbox.io/s/vigilant-sea-3q8v08

if i try to enter in MainLayout, i can enter with ctrl+leftclick, but with npm start, this erro shows:如果我尝试在 MainLayout 中输入,我可以使用 ctrl+leftclick 输入,但使用 npm start 时,此错误显示:

Could not find dependency: 'layouts' relative to '/src/App.tsx'找不到依赖项:相对于“/src/App.tsx”的“布局”

this work: import { MainLayout } from "./components/layouts";这项工作:从“./components/layouts”导入{ MainLayout };

but i want: import { MainLayout } from "layouts";但我想要:从“布局”导入 { MainLayout };

my app我的应用

import React from "react";
import "./App.css";
import { Routes, Route, Link } from "react-router-dom";
import { MainLayout } from "layouts";

function App() {
  return (
    <Routes>
      <Route path="/" element={<MainLayout />}>
        <Route path="/" element={<h1>project with Api</h1>} />
        <Route path="about" element={<h1>test</h1>} />
      </Route>
    </Routes>
  );
}

export default App;

Add baseUrl and remove "./src"s in your tsconfig file.在 tsconfig 文件中添加 baseUrl 并删除“./src”。

.
.
    "baseUrl": ".",
    "paths": {
      "components": [
        "./components"
      ],
      "layouts": [
        "./components/layouts"
      ],
.
.

path usage路径使用

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

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