本案例的效果是点击默认页面index.wxml的文字后跳转到另一个自定义页面show.wxml。案例效果如下:
点击前
点击后
制作步骤:
1. 在微信开发者工具中,打开app.json文件,在pages数组中增加show.wxml页面相关文件的代码,以加粗显示,代码如下:
{
"pages":[
"pages/index/index",
"pages/show/show",
"pages/logs/logs"
],
"window":{
"backgroundTextStyle":"light",
"navigationBarBackgroundColor": "#ccc",
"navigationBarTitleText": "WeChat",
"navigationBarTextStyle":"black"
}
}
2. 在index.wxml文件中,在类为usermotto的view组件中添加绑定属性catchtap='enterShow',以加粗显示,代码如下:
<!--index.wxml-->
<view class="container">
<view class="userinfo">
<button wx:if="{{!hasUserInfo && canIUse}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 获取头像昵称 </button>
<block wx:else>
<image bindtap="bindViewTap" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" background-size="cover"></image>
<text class="userinfo-nickname">{{userInfo.nickName}}</text>
</block>
</view>
<view class="usermotto" catchtap='enterShow'>
<text class="user-motto">{{motto}}</text>
</view>
</view>
3. 在index.js文件中,将data中motto的值改为“点击进入”。编写实现跳转的自定义函数enterShow,加粗显示,代码如下:
//index.js
//获取应用实例
const app = getApp()
Page({
data: {
motto: '点击进入',
userInfo: {},
hasUserInfo: false,
canIUse: wx.canIUse('button.open-type.getUserInfo')
},
//事件处理函数
enterShow:function(){
wx.navigateTo({
url: '../show/show',
})
4. 在show.wxml中,输入跳转后页面显示的信息,代码如下:
<view>
<text>这是跳转后的页面</text>
</view>
5. 然后在index.xwml中点击测试就可以了。
说明:在上面的页面跳转自定义函数enterShow中,也可以使用wx.redirectTo实现跳转。两者的区别:redirectTo将关闭当前页面,跳转到指定页面,页面左上角没有返回的箭头按钮;而navigateTo将保留页面,跳转到指定页面,页面左上角有返回的箭头按钮。