【发布时间】:2020-09-29 22:00:04
【问题描述】:
在我的 pages/screens 文件夹中,我创建了一个索引文件并执行了如下操作:
export * from './UserPage';
export * from './users/SearchPage';
所以现在,当我想从我的 App.tsx 文件中的这些页面导入组件时,我可以
import { SearchPage, NewPage } from './pages';
而不是输入完整的相对路径。但是,我想使用类似的东西
import { SearchPage, NewPage } from '~/pages';
为此,我将其添加到我的 tsconfig.json 文件中,因为它似乎在另一个项目中有效,但在这里对我不起作用:
"paths": {
"~/*": ["./*"],
"~": ["./"]
},
我还能如何更改我的导入格式?
编辑: tsconfig.json:
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "preserve",
"baseUrl": "./src",
"paths": {
"@modules/*": ["modules/*"],
"@config/*": ["config/*"],
"@shared/*": ["shared/*"]
},
},
"include": [
"src"
]
}
【问题讨论】:
-
为什么你的主目录中有包文件?为什么要将主目录的内容公开给 HTTP 请求?您的网络服务器是否在您的个人用户下运行?这是一件非常奇怪的事情......
-
这并不是一件奇怪的事情。波浪号通常在导入路径中用作项目根目录(或根源目录)的简写。
标签: javascript reactjs typescript react-native webpack