javascript - 它无法识别导入文件中的文件

标签 javascript react-native import frontend navigation-drawer

我创建了一个 MenuButton 以及其他 2 个页面,其中一个是设置屏幕,我在两个文件中导入了 MenuButton,它们似乎工作正常。但是当我在 DrawerNavigator 文件中导入设置 Screen 时,它无法识别 MenuButton

Failed to load bundle(http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false) with error:(Unable to resolve module `./Menu/MenuButton` from `/Users/camillebasbous/Project/Menu/SettingScreen.js`: The module `./Menu/MenuButton` could not be found from `/Users/camillebasbous/Project/Menu/SettingScreen.js`. Indeed, none of these files exist:
  * `/Users/camillebasbous/Project/Menu/Menu/MenuButton(.native||.ios.js|.native.js|.js|.ios.json|.native.json|.json|.ios.ts|.native.ts|.ts|.ios.tsx|.native.tsx|.tsx)`
  * `/Users/camillebasbous/Project/Menu/Menu/MenuButton/index(.native||.ios.js|.native.js|.js|.ios.json|.native.json|.json|.ios.ts|.native.ts|.ts|.ios.tsx|.native.tsx|.tsx)` (null))

__38-[RCTCxxBridge loadSource:onProgress:]_block_invoke.228
    RCTCxxBridge.mm:414
___ZL36attemptAsynchronousLoadOfBundleAtURLP5NSURLU13block_pointerFvP18RCTLoadingProgressEU13block_pointerFvP7NSErrorP9RCTSourceE_block_invoke.118
__80-[RCTMultipartDataTask URLSession:streamTask:didBecomeInputStream:outputStream:]_block_invoke
-[RCTMultipartStreamReader emitChunk:headers:callback:done:]
-[RCTMultipartStreamReader readAllPartsWithCompletionCallback:progressCallback:]
-[RCTMultipartDataTask URLSession:streamTask:didBecomeInputStream:outputStream:]
__88-[NSURLSession delegate_streamTask:didBecomeInputStream:outputStream:completionHandler:]_block_invoke
__NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__
-[NSBlockOperation main]
-[__NSOperationInternal _start:]
__NSOQSchedule_f
_dispatch_call_block_and_release
_dispatch_client_callout
_dispatch_continuation_pop
_dispatch_async_redirect_invoke
_dispatch_root_queue_drain
_dispatch_worker_thread2
_pthread_wqthread
start_wqthread

我已经尝试测试其他页面,经过几次测试后,我意识到这些页面中导入的 MenuButton 的存在导致了错误是否有办法导入已导入另一个要显示的文件或我是否必须在抽屉导航中导入它们,如果是的话如何构建代码。谢谢

抽屉导航代码:

import * as React from 'react';
import { Text, View, Image, ScrollView, StyleSheet } from 'react-native';
import {
  createDrawerNavigator,
  createAppContainer,
  DrawerItems,
  SafeAreaView,
} from 'react-navigation';
import SettingScreen from './Menu/SettingScreen'





class Home extends React.Component {
  static navigationOptions = {
    title: 'Home',

  };

  render() {
    return (
      <View style={styles.container}>

        <SettingScreen/>

      </View>
    );
  }
}



const Navigator = createDrawerNavigator(
  {
    Home,

  },
  {
    //drawerType: 'back',
    // drawerPosition: 'right',
    // drawerWidth: 200,
  drawerBackgroundColor: '#262A2C',
    // contentComponent: CustomDrawerContentComponent
  }
);

 export default createAppContainer(Navigator);

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    backgroundColor: '#ecf0f1',

  }
});

设置屏幕代码:

import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import MenuButton from './Menu/MenuButton'

export default class SettingScreen extends React.Component{
    render(){
        return(
            <View style={styles.container}>
            <MenuButton/>
            <Text style={styles.text}>Settings</Text>
            </View>
        )
    }
}
const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: 'rgba(215,215,215,1)',
        alignItems: 'center',
        justifyContent: 'center',
    },
text:{
    fontSize: 30,
    }
});

菜单按钮代码:

import React from 'react';
import {AppRegistry, StyleSheet, View} from "react-native" ;
import Icon from 'react-native-vector-icons/Ionicons'
import {widthPercentageToDP as wp, heightPercentageToDP as hp} from 'react-native-responsive-screen'



export default class MenuButton extends React.Component {
    render() {
        return(

        <View >

        <Icon name= "ios-menu" size={wp('12%')} color='#9B9B9B' style={{position: 'absolute', top: wp('-82.5%'), left: wp('-46%'), }}></Icon>

                 </View>
        ) 
    }
}


AppRegistry.registerComponent('Menu', () => FixedDimensionsBasics);

最佳答案

您在导入时使用的路径是相对于您的文件的。因此,由于所有内容都位于同一文件夹中,因此您必须像这样更正导入路径:

  1. 抽屉导航器代码

    import SettingScreen from './SettingScreen'
    
  2. 设置屏幕代码

    import MenuButton from './MenuButton'
    

关于javascript - 它无法识别导入文件中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55858705/

相关文章:

mysql - 从 --all-databases 转储导入单个数据库

从 .sql 文件导入到 cassandra 表

javascript - unicode字符串中每个字符的二进制数据如何与下一个字符的二进制数据分开?

javascript - 使用 Switch 语句中单击的按钮

javascript - jQuery:根据窗口宽度更改 jQuery

android - 有没有办法检查用户是否已经安装了该应用程序

javascript - $.each() 中的记录顺序

javascript - React Native,无法启动模拟器。原因 : No emulators found as an output

javascript - React Native - 如何从 ScrollView 获取 View 的 Y 偏移值?

mysql - 使用 PDO 将 XML 文件导入 mySQL