我正在设置一个新的 React-Native 项目,并希望通过将 jsconfig.json 文件添加到项目的根目录来配置应用程序以支持使用绝对路径导入模块。但该应用程序无法解析这些模块。
我需要做一些额外的设置吗?
我使用react-native-cli 2.0.1创建了一个新的React-Native项目,其结构如下,并尝试在App.js中导入MyButton,如下所示:`import MyButton from '~/components/MyButton';`
|-- android
|-- ios
|-- node_modules
|-- src
|-- components
|-- MyButton.js
|-- App.js
|-- .eslintrc.js
|-- .flowconfig
|-- .watchmanconfig
|-- app.json
|-- babel.config.json
|-- index.js
|-- jsconfig.json
|-- metro.config.js
|-- package.json
jsconfig.json:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"~/*": ["src/*"]
}
}
}
包.json
{
"name": "TestApp",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "react-native start",
"test": "jest",
"lint": "eslint ."
},
"dependencies": {
"react": "16.8.6",
"react-native": "0.60.3"
},
"devDependencies": {
"@babel/core": "^7.5.4",
"@babel/runtime": "^7.5.4",
"@react-native-community/eslint-config": "^0.0.5",
"babel-jest": "^24.8.0",
"eslint": "^6.0.1",
"jest": "^24.8.0",
"metro-react-native-babel-preset": "^0.55.0",
"react-test-renderer": "16.8.6"
},
"jest": {
"preset": "react-native"
}
}
我希望该应用程序能够解析该模块,但我收到错误:
`Error: Unable to resolve module `~/components/MyButton` from `/TestApp/src/App.js`: Module `~/components/MyButton` does not exist in the Haste module map