Navigator 跳转页面

  • Arans
  • 11 Minutes
  • June 24, 2018

初始导航器 [ 理论 ]

1
2
3
4
5
6
7
8
9
10
11
12
13
return (
<Navigator
//初始化组件
initialRoute={{ name: defaultName, component: defaultComponent }}
//页面切换效果
configureScene={(route) => {
return Navigator.SceneConfigs.VerticalDownSwipeJump; //定义了跳转方式
}}
renderScene={(route, navigator) => {
let Component = route.component;
return <Component {...route.params} navigator={navigator} />
}} />
)

initialRoute主要定义了首个加载项,

跳转方式

1
2
3
4
5
renderScene={(route, navigator) => {
let Component = route.component;
return <Component {...route.params} navigator={navigator} />
}} />
);

route里主要存储的就是我们传递的name,component

navigator是一个”Navigator对象” 主要为我们提供push pop jump… 多种方法

navigator:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**来自IOS的示例代码*/
class MyView extends Component {
_handleBackPress() {
this.props.navigator.pop();
}

_handleNextPress(nextRoute) {
this.props.navigator.push(nextRoute); //用push()方法把准备好的数据推到下一个页面
}

render() {
const nextRoute = { //此处创建了一个nextRoute对象用于存储将要传递的数据
component: MyView, //'component'定义了将要跳转的组件名
title: 'Bar That', //设置标题栏
params:{ aran:"this is aran"} //准备了将要传递的数据
};
return(
<TouchableHighlight onPress={() => this._handleNextPress(nextRoute)}> //在此传递数据
<Text style={{marginTop: 200, alignSelf: 'center'}}>
See you on the other nav {this.props.myProp}!
</Text>
</TouchableHighlight>
);
}
}

初始导航器 [ 实践]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**page1**/
jumpClick(){
const{navigator} = this.props;
/*that存储了上一个this*/
if(navigator){
/*push主要掌管页面跳转 返回的component属性决定了呈现哪一个class*/
navigator.push({
name : "SecondPageComponent",
component : TheNextPage,
params:{ aran:"this is aran"}
})
}
}

render() {
return(
<TouchableHighlight onPress={this.jumpClick.bind(this)}>
</TouchableHighlight>
)
}
1
2
3
4
5
6
7
8
/**2**/
return(
<View>
<Text>
{this.props.aran}
</Text>
</View>
)

{this.props.aran} 接受上面传回的key

success

-