当前位置:   article > 正文

微信小程序(四十八)登入页面跳转与回跳

微信小程序(四十八)登入页面跳转与回跳

注释很详细,直接上代码

上一篇

新增内容:
1.在组件中获取当前页面的路由并通过参数传递给登入页面
2.提取传递的参数并在登入成功以后从登入页面回跳到原页面

源码

app.js

App({
  globalData:{//定义全局变量
     isLoad:false
  }
 })
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

app.json

{
  "usingComponents": {
    "auth":"/components/auth/auth"
  },
  "pages": [
    "pages/index/index",
    "pages/begin/begin",
    "pages/start/start"
  ]
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

/components/auth/auth.wxml

<!-- 使用properties参数(是否登入) -->
<slot wx:if="{{isLoad}}"></slot>

  • 1
  • 2
  • 3

/components/auth/auth.js

Component({
  behaviors: [],
  properties: {
    isLoad: {//接收参数(是否登入)
      type: Boolean,
      value: false
    }
  },
  lifetimes: {
    created() {//因为功能限制太多了,所以一般都不用这个
      
    },
    attached() {//这个比较常用
      const app=getApp()

      //如果没有登入
      if(!app.globalData.isLoad){
        const nowPages=getCurrentPages()
        const {route}=nowPages.pop()

        wx.redirectTo({//带参重定向
          url: '/pages/index/index?redirectUrl=/'+route,
        })

        //不延时弹窗提示会卡没
        setTimeout(()=>{
          wx.showToast({
            title: '未登入,已跳转到登入页面,请先登入!',
            icon:'none'
          })
        },1000)
        
      }
    },
    moved() {

    },
    detached() {

    },
  methods: {
    
  }
}
})


  
  • 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
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

start.wxml

<button bind:tap="onTap" type="primary">进入需要登入才能看的页面</button>
  • 1

start.js

// pages/start/start.js
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onTap(){
    wx.redirectTo({//跳转到登入页面
      url: '/pages/begin/begin',
    })
  }
})
  • 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
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71

begin.wxml

<auth isLoad="{{isLoad}}"><!-- 传输参数(是否登入) -->
  <view class="tip">
  登入以后可以查看的内容
  </view>
</auth>
  • 1
  • 2
  • 3
  • 4
  • 5

begin.wxss

.tip{
  font-size: 60rpx;
  color:palegreen; 
  margin-top: 200rpx;
  padding: 0rpx 30rpx;
  background-color: bisque;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

begin.js

// pages/begin/begin.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //参数:是否登入
    isLoad:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
   //进入这个页面同步登入状态
   const app=getApp()

   this.setData({
     isLoad:app.globalData.isLoad
   })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
  • 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
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72

index.wxml

<button type="primary" bind:tap="inLoad">登入</button>
  • 1

index.js

Page({
  data:{
    isLoad:false,
    redirectUrl:''
  },
  onLoad({redirectUrl}){//接收并解构重定向参数
    // console.log(redirectUrl)

    this.setData({//存储跳转之前的页面
      redirectUrl:redirectUrl
    })
  },

  //登入
  inLoad(){
    //修改全局变量为true
    const app=getApp()
    app.globalData.isLoad=true
    //console.log(app.globalData.isLoad)
    
    this.setData({//修改页面数据
      isLoad:app.globalData.isLoad
    })

    //登入以后跳转到之前的页面
    wx.redirectTo({
      url: this.data.redirectUrl
     })
  }
})

  • 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
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

效果演示

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/203732
推荐阅读
相关标签
  

闽ICP备14008679号