1. 跳转的几种方式

    // 参数
    var Object = {
      url: '',  
      delta: 1, // 返回的页面数,如果 delta 大于现有历史页面数,则返回到首页。(只有 wx.navigateBack 生效)
      success: function(){}, 
      fail: function(){}, 
      complete: function(){}, 
    }
    
    // 保留当前页面、跳转到某个页面
    wx.navigateTo(Object);
    
    // 关闭当前页面,并返回到上一级页面或上多级页面(返回到历史页面)
    wx.navigateBack(Object);
    
    // 关闭当前页面,跳转到某个页面
    wx.redirectTo(Object);
    
    // 关闭所有页面,打开某个页面,返回的时候跳到首页
    wx.reLaunch(Object);
    
    // 关闭所有页面,跳转到 tabBar 页面
    wx.switchTab(Object);
  2. 跳转传参和获取参数(1)

    // a page wxml
    <view class="cont-title" bindtap="openArticle" id="111" data-title='文章标题'>文章标题</view>
    
    // a page js
    // 文章跳转
      openArticle(e) {
        wx.navigateTo({
        // id 可以直接写,获取用e.currentTarget.id,其它参数需要使用 data- 前缀,获取用 e.currentTarget.dataset.title
          url: `../article/article?atcId=${e.currentTarget.id}&title=${e.currentTarget.dataset.title}`,
          // events: events,
          success: (result) => { },
          fail: (res) => { },
          complete: (res) => { },
        })
      },
      
     // b page 
     onLoad(options) {
        console.log(options.id, options.title);
      },
  3. 跳转传参和获取参数(2)

    // a page wxml
    <view class="cont-title" bindtap="openArticle" id="111" data-title='文章标题'>文章标题</view>
    
    // a page js
    // 文章跳转
      openArticle(e) {
        wx.navigateTo({
          url: '/pages/detail/index',
          success: function(res) {
            const data = {
              id: e.currentTarget.dataset.id,
              title: '标题',
            }
            // 发送事件,携带参数
            res.eventChannel.emit('acceptDataFromOpenerPage', { data })
          },
          fail: function(err) {},
          complete: function() {},
        })
      },
      
     // b page 
    onLoad(options) {
        const eventChannel = this.getOpenerEventChannel()
        // 监听事件,获取参数
        eventChannel.on('acceptDataFromOpenerPage', function(data) {
          console.log(data.data.id,data.data.title)
        })
      }
最后修改:2024 年 01 月 11 日
如果觉得我的文章对你有用,请随意赞赏