首先进入到项目的跟路径然后加载 react-navigation 第三方库 如图
代码中引入StackNavigator组件
代码实现分三个js页面实现,index.ios.js MainVC.js首页 DetailVC.js详情页面
首先来看一下 index.ios.js实现代码
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
TouchableOpacity
} from 'react-native';
import {
StackNavigator,
} from 'react-navigation';
import DetailVC from './DetailVC';
import MainVC from './MainVC';
// StackNavigator配置,默认显示MianVC页面
const MyNavigatior = StackNavigator(
{
MainVC: {screen: MainVC },
DetailVC: {screen: DetailVC},
},
{
initialRouteName: 'MainVC',//默认路由,就是第一个要显示的页面
}
);
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#F5FCFF',
},
});
AppRegistry.registerComponent('MyNavigatior', () => MyNavigatior);
然后看MainVC.js首页实现代码
mport React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
TouchableOpacity
} from 'react-native';
export default class MainVC extends Component {
static navigationOptions = {
title: '首页'//对页面的配置
};
render() {
const { navigate } = this.props.navigation;
return (
<View style={styles.container}>
<TouchableOpacity style={{height:60,backgroundColor:'orange',justifyContent: 'center',}}
onPress={() => navigate('DetailVC', { title: '详情',des:'我是返回点击我' })} >
<Text>点击进详情页</Text>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#F5FCFF',
},
});
在看DetailVC.js首页实现代码
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
TouchableOpacity
} from 'react-native';
export default class DetailVC extends Component {
//接收上一个页面传过来的title显示出来
static navigationOptions = ({ navigation }) => ({
title: navigation.state.params.title
});
// 点击返回上一页方法
backVC=()=>{
//返回首页方法
this.props.navigation.goBack();
}
render() {
const { navigate } = this.props.navigation;
return (
<View style={styles.container}>
<TouchableOpacity style={{
height:40,
backgroundColor:'green',
justifyContent: 'center'}}
onPress={() =>{this.backVC()}}>
<Text>{this.props.navigation.state.params.des}</Text>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#F5FCFF',
},
});
最看看一下实现的效果图
iOS
Android