将绝对路径添加到由"Create React App"创建的应用中; [英] Add absolute paths to app created by "Create React App"

查看:47
本文介绍了将绝对路径添加到由"Create React App"创建的应用中;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过创建React App 创建了一个应用,更具体地说,我我也在使用打字稿.

I have created an app by Create React App and to be more specific I'm also using typescript.

我无法确定如何设置访问我的组件,页面等的绝对路径.

I can't figerout how to set absolute paths to access to my components, pages, etc..

在另一种情况下,我将使用以下内容更新我的tscongig:

In a different scenario I would update my tscongig with something like:

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

但是我不知道如何实现,因为我正在使用 react-scripts 有什么主意吗?

but I have no idea how to implement this as I'm using react-scripts any idea?

推荐答案

创建tsconfig.json文件并添加以下代码.

Create a tsconfig.json file and add the code below.

{
  "compilerOptions": {
    "baseUrl": "src"
  },
  "include": [
    "src"
  ]
}

然后,您可以将组件导入为

Then you can import your components as

import Header from 'components/Header';

这篇关于将绝对路径添加到由"Create React App"创建的应用中;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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