小程序之页面跳转

小程序源码目录。
在这里插入图片描述

标签页与标签页之间的跳转

标签页与标签页之间的跳转,通过wx.switchTab()实现。

wx.switchTab()
  • app.json
{
  "pages":[
    "pages/index/index",
    "pages/person/person"
  ],
  "window":{
    "backgroundTextStyle":"light",
    "navigationBarBackgroundColor": "#fb3",
    "navigationBarTitleText": "Weixin",
    "navigationBarTextStyle":"black"
  },
  "tabBar": {
    "color": "#101010",
    "selectedColor": "#f9b05c",
    "list": [
      {
        "iconPath": "./images/home.png",
        "selectedIconPath": "./images/home_select.png",
        "pagePath": "pages/index/index",
        "text": "首页"
      },
      {
        "iconPath": "./images/me.png",
        "selectedIconPath": "./images/me_select.png",
        "pagePath": "pages/person/person",
        "text": "个人中心"
      }
    ]
  },
  "style": "v2",
  "sitemapLocation": "sitemap.json"
}
  • pages\index\index.wxml
<!--index.wxml-->
<button bindtap="switchToPerson">跳到 个人中心</button>
  • pages\index\index.js
// index.js
Page({
  switchToPerson:function(){
    console.log("begin to switch")
    wx.switchTab({
      url: '/pages/person/person',
      success:(res) => {
        console.log(res);
      }
    })
  }
})
  • pages\person\person.wxml
<!--pages/person/person.wxml-->
<button bindtap="switchToIndex">跳回 首页</button>
  • pages\person\person.js
// pages/person/person.js
Page({
  switchToIndex:function(){
    wx.switchTab({
      url:"/pages/index/index",
      success:res => {
        console.log(res);
      }
    })
  }
})

在这里插入图片描述

非标签页与非标签页之间的跳转

非标签页与非标签页之间的跳转,可以通过wx.navigateTo()wx.redirectTo()实现。其中,

wx.navigateTo()

将跳转至目标页面,且保留当前页面。跳转后,单击左上角可以返回到上一页面。
也可以通过wx.navigateBack()返回到原页面。
wx.navigateTo()只能跳转到非标签页。

wx.redirectTo()

将跳转到目标页面,且关闭当前页面。跳转后,不能返回到上一页面。
wx.redirectTo()只能跳转到非标签页。

  • app.json
{
  "pages":[
    "pages/index/index",
    "pages/person/person",
    "pages/address/address",
    "pages/order/order",
    "pages/detail/detail"
  ],
  "window":{
    "backgroundTextStyle":"light",
    "navigationBarBackgroundColor": "#fb3",
    "navigationBarTitleText": "Weixin",
    "navigationBarTextStyle":"black"
  },
  "tabBar": {
    "color": "#101010",
    "selectedColor": "#f9b05c",
    "list": [
      {
        "iconPath": "./images/home.png",
        "selectedIconPath": "./images/home_select.png",
        "pagePath": "pages/index/index",
        "text": "首页"
      },
      {
        "iconPath": "./images/me.png",
        "selectedIconPath": "./images/me_select.png",
        "pagePath": "pages/person/person",
        "text": "个人中心"
      }
    ]
  },
  "style": "v2",
  "sitemapLocation": "sitemap.json"
}
  • pages\person\person.wxml
<!--pages/person/person.wxml-->
<view class="item info" bindtap="gotoInfo">
  <view>个人资料</view>
  <image src="../../images/arrow.png"></image>
</view>
<view class="item order" bindtap="gotoOrder">
  <view>订单物流查询</view>
  <image src="../../images/arrow.png"></image>
</view>
<view class="item order" bindtap="gotoAddress">
  <view>选择收货地址</view>
  <image src="../../images/arrow.png"></image>
</view>
  • pages\person\person.wxss
/* pages/person/person.wxss */
image{
  width: 32rpx;
  height: 32rpx;
}
.item{
  margin: 50rpx;
  padding-bottom: 30rpx;
  border-bottom: 1px solid #eee;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12pt;
}
  • pages\person\person.js
// pages/person/person.js
Page({
  gotoInfo:function(){
    wx.navigateTo({
      url: '/pages/detail/detail',
    })
  },
  gotoOrder:function(){
    wx.navigateTo({
      url: '/pages/order/order',
    })
  },
  gotoAddress:function(){
    wx.redirectTo({
      url: '/pages/address/address'
    })
  }
})
  • pages\detail\detail.wxml
<!--pages/detail/detail.wxml-->
<open-data type="userAvatarUrl"></open-data>
  • pages\order\order.wxml
<!--pages/order/order.wxml-->
<button bindtap="goBack" type="primary">返回</button>
  • pages\order\order.wxss
/* pages/order/order.wxss */
button{
  margin-top: 50rpx;
}
  • pages\order\order.js
// pages/order/order.js
Page({
  goBack:function(){
    wx.navigateBack({
      delta: 0,
    })
  }
})
  • pages\address\address.wxml
<!--pages/address/address.wxml-->
<form>
  <input type="text" placeholder="请输入您的收货地址" />
</form>
  • pages\address\address.wxss
/* pages/address/address.wxss */
input{
  margin: 30rpx;
  padding: 8rpx;
  border: 1px solid #fb3;
  border-radius: 6rpx;
  line-height: 5vh;
  height: 5vh;
}

在这里插入图片描述

标签页与非标签页之间的跳转
wx.reLaunch()

wx.reLaunch()将关闭所有页面,打开应用中的某个页面。既可以跳转到标签页,也可以跳转到非标签页。

  • pages\index\index.wxml
<!--index.wxml-->
<button bindtap="gotoPerson">去个人中心</button>
<button bindtap="gotoAddress">去选择收货地址</button>
  • pages\index\index.js
// index.js
Page({
  gotoPerson:function(){
    wx.reLaunch({
      url: '/pages/person/person',
      success:() => {
        console.log("we got to person page");
      }
    })
  },
  gotoAddress:function(){
    wx.reLaunch({
      url: '/pages/address/address',
      success:() => {
        console.log("we got to address page");
      }
    })
  }
})

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值