Javascript 使用npm在React Native中使用作用域导入
我试图改变在React Native中导入模块的方式。这样做的标准方法如下:Javascript 使用npm在React Native中使用作用域导入,javascript,reactjs,typescript,react-native,ecmascript-6,Javascript,Reactjs,Typescript,React Native,Ecmascript 6,我试图改变在React Native中导入模块的方式。这样做的标准方法如下: import HomeScreen from "../../screens/Home" 我也可以这样做: import HomeScreen from "@screens/Home" 为此,我尝试在screens文件夹中创建一个package.json文件并将其命名 { "name": "@screens" } 但它不起作用 更新: 我尝试使用babel插件模块解析器,并使用.babelrc和.tsconf
import HomeScreen from "../../screens/Home"
我也可以这样做:
import HomeScreen from "@screens/Home"
为此,我尝试在screens文件夹中创建一个package.json文件并将其命名
{
"name": "@screens"
}
但它不起作用
更新:
我尝试使用babel插件模块解析器,并使用.babelrc和.tsconfig对其进行配置,但问题仍然存在
.babelrc
tsconfig.json
PS:我也在使用Typescript你用什么来加载模块?我在使用babel,带有“react native”预设。然后.babelrc文件就是你设置别名的地方。本文可能对您有所帮助:模块名称上的
@
符号用于。这是我播撒他们使用这个符号的项目,这对我来说很奇怪,因为。babelrc是标准的
{
"plugins": [
[
"module-resolver",
{
"root": ["./src"],
"alias": {
"test": "./test",
"underscore": "lodash"
}
}
]
]
}
{
"compilerOptions": {
"allowJs": false,
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"jsx": "react-native",
"module": "es2015",
"moduleResolution": "node",
"outDir": "build",
"rootDir": "src",
"noImplicitAny": false,
"noImplicitReturns": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"sourceMap": true,
"target": "es2015",
"lib": ["es2015", "es2017", "dom"],
"skipLibCheck": true,
"baseUrl": ".",
"paths": {
"*": ["src/*"],
"test/*": ["test/*"],
"underscore": ["lodash"]
}
},
"exclude": ["node_modules"],
"include": ["src"]
}