這次帶給大家的是導航元件react-navigation如何使用,大家應該都有所體會,我們在一般應用都有跨tab跳轉的需求, 這就需要特別處理下路由,這篇文章就給大家好好分析一下。
特定情境是: app分三大模組Home首頁, Bill帳單和Me我的, 對應三個tab. 現在需求是 Home push HomeTwo, HomeTwo push BillTwo, BillTwo 返回到Bill賬單首頁.
const Components = { HomeTwo: { screen: HomeTwo, path:'app/HomeTwo' }, HomeThree: { screen: HomeThree, path:'app/HomeThree' }, BillTwo: { screen: BillTwo, path:'app/BillTwo' }, BillThree: { screen: BillThree, path:'app/BillThree' }, } const Tabs = TabNavigator({ Home: { screen: Home, path:'app/home', navigationOptions: { tabBar: { label: '首页', icon: ({tintColor}) => (<Image source={require('./images/home.png')} style={[{tintColor: tintColor},styles.icon]}/>), }, } }, Bill: { screen: Bill, path:'app/bill', navigationOptions: { tabBar: { label: '账单', icon: ({tintColor}) => (<Image source={require('./images/bill.png')} style={[{tintColor: tintColor},styles.icon]}/>), }, } }, Me: { screen: Me, path:'app/me', navigationOptions: { tabBar: { label: '我', icon: ({tintColor}) => (<Image source={require('./images/me.png')} style={[{tintColor: tintColor},styles.icon]}/>), }, } } }, { tabBarPosition: 'bottom', swipeEnabled: false, animationEnabled: false, lazyLoad: false, backBehavior: 'none', tabBarOptions: { activeTintColor: '#ff8500', inactiveTintColor: '#999', showIcon: true, indicatorStyle: { height: 0 }, style: { backgroundColor: '#fff', }, labelStyle: { fontSize: 10, }, }, }); const Navs = StackNavigator({ Home: { screen: Tabs, path:'app/Home' }, Bill: { screen: Tabs, path:'app/Bill' }, Me: { screen: Tabs, path:'app/Me' }, ...Components }, { initialRouteName: 'Home', navigationOptions: { header: { style: { backgroundColor: '#fff' }, titleStyle: { color: 'green' } }, cardStack: { gesturesEnabled: true } }, mode: 'card', headerMode: 'screen' });
在HomeTwo裡使用react-navigation自帶的reset action就可以重置路由信息了:
// push BillTwo this.props.navigation.dispatch(resetAction); // 使用reset action重置路由 const resetAction = NavigationActions.reset({ index: 1, // 注意不要越界 actions: [ // 栈里的路由信息会从 Home->HomeTwo 变成了 Bill->BillTwo NavigationActions.navigate({ routeName: 'Bill'}), NavigationActions.navigate({ routeName: 'BillTwo'}) ] });
從HomeTwo push到BillTwo頁面後, 點擊BillTwo的左上角導航按鈕返回就能返回到Bill賬單首頁了.
相信看了以上介紹你已經掌握了方法,更多精彩請關注php中文網其它相關文章!
相關閱讀:
以上是導覽組件react-navigation如何使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!