如何在同一目录中正确导入屏幕?

时间:2019-02-06 06:44:00

标签: javascript android react-native expo

我是react-native/expo的新手,目前正在尝试测试代码,并确保已正确设置react-navigation。当我尝试在智能手机上运行EXPO时,我会得到以下提示:

  

无法从以下位置解析模块“ ./screens/RegistrationScreen”   'C:\ Users [用户名] \ Desktop \ JDA \ Smartbox \ screens \ HomeScreen.js':   找不到模块'./screens/RegistrationScreen   “ C:\ Users [用户名] \ Desktop \ JDA \ Smartbox \ screens \ HomeScreen.js”。   确实,这些文件都不存在...

我不确定我是否在错误地编写目录,但是我似乎无法找到许多其他情况,也无法找到适当的解决方案。

import React from 'react';
import {
  Image,
  Platform,
  ScrollView,
  StyleSheet,
  StatusBar,
  Text,
  Button,
  TouchableOpacity,
  View,
} from 'react-native';
import { WebBrowser } from 'expo';

import { MonoText } from '../components/StyledText';

import {createStackNavigator} from 'react-navigation';

import RegistrationScreen from './screens/RegistrationScreen';

export default class HomeScreen extends React.Component {
  static navigationOptions = {
    header: null,
  };

  onPress = () => {
    this.props.navigation.navigate('Home')
  };

  render() {
    return (
      <View style={{flex: 1}}>
      <View style={[{flex: 1}, styles.container]}>
      <Text style={styles.getStartedText}>Manage my locks</Text>
      </View>
      <View style={{flex: 2}}/>
      <Button style={{flex: 1}}title="Registration Test" onPress={this.onPress}>
      </Button>
      <Button style={{flex: 1}} title="Add new lock">
      </Button>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    ...Platform.select({
      android: {
        marginTop: StatusBar.currentHeight + 15
      }
    })
  },
  developmentModeText: {
    marginBottom: 20,
    color: 'rgba(0,0,0,0.4)',
    fontSize: 14,
    lineHeight: 19,
    textAlign: 'center',
  },
  contentContainer: {
    paddingTop: 30,
  }
});

createStackNavigator({
  Register: {
    screen: RegistrationScreen
  },
  Home: {
    screen: HomeScreen
  }
});

RegistrationScreen的标题如下:

import React from 'react';
import {
    AppRegistry,
    Button,
    StyleSheet,
    Text,
    TextInput,
    TouchableOpacity,
    View

} from 'react-native';

import {createStackNavigator} from 'react-navigation';

import HomeScreen from './screens/HomeScreen';

主屏幕和注册屏幕在同一目录中。我只是在做测试,但是我想确保一旦按下按钮,就可以从该屏幕导航到注册。

1 个答案:

答案 0 :(得分:0)

由于您的HomeScreen和RegistrationScreen位于同一目录中,因此您应导入HomeScreen

import HomeScreen from './HomeScreen';

代替

import HomeScreen from './screens/HomeScreen';

和RegistrationScreen应该是

import RegistrationScreen from './RegistrationScreen';

代替

import RegistrationScreen from './screens/RegistrationScreen';