我已经在navigator.js文件中定义了所有导航器。我要导入它。我如何做到这一点而不会出现任何错误?这是我的navigator.js文件。
import React, {Component} from 'react';
import {
Platform,
View,
Button,
SafeAreaView,
} from 'react-native';
import {
createAppContainer,
createDrawerNavigator,
createBottomTabNavigator,
createStackNavigator,
DrawerItems,
} from 'react-navigation';
/** NAVIGATORS DEFINED **/
const WelcomeTabNavigator = createBottomTabNavigator({
Welcome: {screen: WelcomeScreen},
Profile,
Settings,
},
{
tabBarOptions: {
activeTintColor: '#fb9800',
inactiveTintColor: '#7e7b7b',
style: { height: 40,backgroundColor: '#fff',borderTopWidth:0.5,borderTopColor: '#7e7b7b' },
labelStyle: {fontSize: 20}
},
navigationOptions:({navigation}) => {
const {routeName} = navigation.state.routes[navigation.state.index];
return {
headerTitle: routeName
};
}
})
const WelcomeStackNavigator = createStackNavigator({
WelcomeTabNavigator: WelcomeTabNavigator
},
{
defaultNavigationOptions:({navigation}) => {
return {
headerLeft: (
<Icon
style={{paddingLeft: 20}}
onPress={() => navigation.openDrawer()}
name="md-menu"
size={30}
/>
)
};
}
})
const AppDrawerNavigator = createDrawerNavigator({
Welcome: {screen: WelcomeStackNavigator},
},
{
contentComponent:(props) => (
<View style={{flex:1}}>
<SafeAreaView forceInset={{ top: 'always', horizontal: 'never' }}>
<DrawerItems {...props} />
<Button
title="Logout"
onPress={() => {
props.navigation.navigate('Home')
}}
/>
</SafeAreaView>
</View>
),
drawerOpenRoute: 'DrawerOpen',
drawerCloseRoute: 'DrawerClose',
drawerToggleRoute: 'DrawerToggle'
})
const AppStackNavigator = createStackNavigator({
Home: {screen: HomeScreen},
Welcome: {screen: AppDrawerNavigator}
});
const AppContainer = createAppContainer(AppStackNavigator);
这是我的app.js文件
import React, {Component} from 'react';
import {
Platform,
View,
Button,
SafeAreaView,
} from 'react-native';
import {
createAppContainer,
createDrawerNavigator,
createBottomTabNavigator,
createStackNavigator,
DrawerItems,
} from 'react-navigation';
import Icon from 'react-native-vector-icons/Ionicons';
import HomeScreen from './screens/HomeScreen.js';
import WelcomeScreen from './screens/WelcomeScreen.js';
import Profile from './screens/ProfileScreen.js';
import Settings from './screens/SettingsScreen.js';
class App extends React.Component {
render() {
return <AppContainer />;
}
}
export default App;
这是我的index.js文件
import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';
AppRegistry.registerComponent(appName, () => App);
我想知道如何导入navigator.js文件并连接这3个文件。我的navigator.js文件包含不同的导航器,例如选项卡导航器,抽屉式导航器,堆栈导航器等。那么如何单次导入所有这些导航器?
答案 0 :(得分:0)
您可以将所有导航器合并在一个页面中,只要您导入所有页面即可。
import WelcomeScreen from './WelcomeScreen.js',
import ProfileScreen from './ProfileScreen.js',
import SettingsScreen from './SettingsScreen.js',
import LogoutScreen from './LogoutScreen.js',
const WelcomeTabNavigator = createBottomTabNavigator({
Welcome: {screen: WelcomeScreen,},
Profile: {screen: ProfileScreen,},
Settings: {screen: SettingsScreen,},
},
const WelcomeStackNavigator = createStackNavigator({
Home: {screen: WelcomeTabNavigator,},
Logout: {screen: LogoutScreen,},
})
class App extends React.Component {
render() {
return <WelcomeStackNavigator />;
}
}
export default App;
答案 1 :(得分:0)
我想告诉您一种非常简单的方法来更新使用导航。
以下是您的 index.js 文件
的示例 import React, { Component } from 'react';
import { StyleSheet, View } from 'react-native';
import { Main } from './navigation/Main';
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
},
});
export class App extends Component {
render() {
return (
<View style={ styles.container }>
<Main />
</View>
);
}
}
这是您的 Main.js ,它是您的导航类。您可以在此处声明所有屏幕。
import {
createStackNavigator,
createAppContainer,
} from 'react-navigation';
import { FirstScreen } from '../screens/FirstScreen';
import { SecondScreen } from '../screens/SecondScreen';
const RootStack = createStackNavigator({
FirstScreen: {
screen: FirstScreen,
key: 'FirstScreen',
navigationOptions: {
gesturesEnabled: false,
},
},
SecondScreen: {
screen: SecondScreen,
key: 'SecondScreen',
navigationOptions: {
gesturesEnabled: false,
},
},
});
export const Main = createAppContainer(RootStack);
现在,您可以轻松地从一个屏幕导航到另一屏幕。
import React, { Component } from 'react';
import {
StyleSheet,
Text,
View,
TouchableOpacity,
} from 'react-native';
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
class FirstScreen extends Component {
onGetStarted() {
const { navigate } = this.props.navigation;
navigate('SecondScreen');
}
render() {
return (
<View style={ styles.container }>
<TouchableOpacity
style={ styles.welcome }
onPress={ () => this.onGetStarted() }
>
<Text>
Welcome to React Native!
</Text>
</TouchableOpacity>
<Text style={ styles.instructions }>This is screen 1</Text>
</View>
);
}
}
export { FirstScreen };