react-native 相对项目路径导入组件
发布时间:2020-12-15 06:44:02 所属栏目:百科 来源:网络整理
导读:在使用 react-native 的时候,经常要自定义很多组件,但是只能使用 ../../../ 的方式,经常不记得这是多深,有没有一个插件,能自动帮我们解决这样的问题?节省我们的时候,也将项目看起来更漂亮一点,答案肯定有的,不然不会有这文章了,好了回归正题。 使
在使用 使用指南我们使用的目标要达到以下的使用效果 // 通常
import SomeExample from '../../../some/example.js';
const OtherExample = require('../../../other/example.js');
// 使用了 Babel-Root-Importer 之后
import SomeExample from '~/some/example.js';
const OtherExample = require('~/other/example.js');
组件安装npm 方式 npm install babel-plugin-root-import --save-dev
yarn 方式 yarn add babel-plugin-root-import --dev
配置增加一个 {
"plugins": [ ["babel-plugin-root-import"] ] }
温馨提示已有项目,记得清理一个缓存(先关闭所有控制侦听程序) watchman watch-del-all
npm start -- --reset-cache
扩展自定义根路径前缀 {
"plugins": [ ["babel-plugin-root-import",{ "rootPathSuffix": "src/js" }] ] }
如何你想修改项目的默认根入口别名,默认 {
"plugins": [ ["babel-plugin-root-import",{ "rootPathPrefix": "@" }] ] }
也可以定义多组别名入口 {
"plugins": [
["babel-plugin-root-import",[{
"rootPathPrefix": "~",// `~` 默认
"rootPathSuffix": "src/js"
},{
"rootPathPrefix": "@","rootPathSuffix": "other-src/js"
},{
"rootPathPrefix": "#","rootPathSuffix": "../../src/in/parent" //也支持父级路径
}]]
]
}
// 然后你就可以这样使用插件了。
import foo from '~/my-file';
const bar = require('@/my-file'); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |