Use absolute path in React components

问题: How I can use absolute path from root and change root, for import components? import Modal from project/app/src/Components/Modal import Main from ../../../../Constants...

问题:

How I can use absolute path from root and change root, for import components?

import Modal from project/app/src/Components/Modal
import Main from ../../../../Constants

and I want, change root directory to I can import from src

import Modal from Components/Modal
import Main from Constants

I try change workspaces in package.json

"workspaces": [
    "project/app/src/*"
]

or use

"scripts": {
    "start": "NODE_PATH=project/app/src/ webpack-dev-server"
}

but it's not working.


回答1:

To implement absolute imports in create-react-app First you need to Create a ‘.env’ file at the root level (same level as package.json) then Set an environment variable, ‘NODE_PATH’ to ‘src/’

NODE_PATH=src
  • 发表于 2019-07-08 00:26
  • 阅读 ( 167 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除