小程序API全局生命周期、路由跳转传值"wx.navigateTo()"

309 阅读2分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第8天,点击查看活动详情 今天学习了很多接口API,本文章没有对所有接口都做解释,大家可以去参考官网,指把我认为重要的做了记录,尤其是路由跳转传值。

生命周期

getLaunchOptionsSync()

  • 作用:获取小程序启动时的参数。与 App.onLaunch 的回调参数一致。
  • 返回值:是个对象,其中对象的query属性,可以接受从一个小程序跳到当前小程序时传来的值。
  • 该方法与全局app.js文件的onLanch(){}生命周期获取到的参数是一样的,只不过onLanch生命周期只能用在app.js里,接口可以用在任何地方

wx.getEnterOptionsSync

  • 作用:获取本次小程序启动时的参数。如果当前是冷启动,则返回值与 App.onLaunch 的回调参数一致;如果当前是热启动,则返回值与 App.onShow 一致。
  • 返回值:是个对象。

应用级事件

wx.onThemeChange(function listener)

  • 只要小程序主题发生变化,里面的函数就会被调用。
  • 里面会接收到参数res,有theme属性
  • 只有在全局配置"darkmode": true时才会触发此事件。

wx.onLazyLoadError(function listener)

作用:可以用来监听异步加载的组件是否加载成功

wx.offThemeChange(function listener)

  • 作用:移除系统主题改变事件的监听函数。
  • 应用级事件当中有很多带有off关键字的事件,所有的带off关键字的事件都是与带on关键字事件是相反操作,都是移除事件回调。移除的与绑定的都是同一个回调。
const listener = function (res) { console.log(res) }

wx.onThemeChange(listener)
wx.offThemeChange(listener) // 需传入与监听时同一个的函数对象

使用wx.navigateTo()可以实现页面跳转间的相互传值。

  • 方法一:实现当前页面向被打开的页面传值,也可以被打开的页面向当前页传值。想什么时候传值,就什么时候调对应的接口即可。

index页面相关代码

//index.wxml
<button bindtap="jumpIndson">跳到ind_son页面</button>
<text>{{indexColor}}</text>
//index.js
Page({
   data:{
     name:'tom',
     indexColor:''
   },
   //按钮点击事件
   jumpIndson:function(){
     //跳转到ind_son页面
      wx.navigateTo({
         url:`/pages/ind_son/ind_/son?name=${this.data.name}`, //跳转到的url
         //接收被打开的页面传过来的数据
         events:{
         //data就是传过来的数据,把它放到data里存储
            indexEvents:data=>{
               this.setData({
                  indexColor:data
               })
            }
         }
      })
   }
})

ind_son页面相关代码

    //ind_son.wxml
<view>{{ind_sonName}}</view> //渲染出的是tom
<button bindtap="chuanIndex">向index页面传值</button>
//ind_son.js
Page({
    data:{
       ind_sonName:'',
       color:'#fcc'
    },
    onLoad:function(options){
    //接收index页面传过来的name值,存到data数据中,渲染到页面上
       this.setData({
          ind_sonName:options.name
       })
    },
    chuanIndex:function(){
     //先获取页面间通信通道对象
       const eventChannel = this.getOpenerEventChannel();
       eventChannel.emit('indexEvents',this.data.color)
    }
})

方法一中的wx.navigateTo()方法中的events属性,是页面间通信接口,用于监听被打开的页面传到当前页的数据

  • 方法二:index页面调用success中的函数接收的参数中的方法,向ind_son页面传值

index页面相关代码

//index.wxml
<button bindtap="jumpIndson">跳到ind_son页面</button>
//index.js
Page({
   data:{
      name:'tom'
   },
   jumpIndson:function(){
     wx.navigateTo({
        url:'/pages/ind_son/ind_son',
        success:(res)=>{
        //这的方法和ind_son.js文件中的eventChannel.on()是相呼应的
           res.eventChannel.emit('indSonEvent',this.data.tom)
        }
     })
   }
})

ind_son页面相关代码

//ind_son.wxml
<view>{{ind_sonName}}</view>
//ind_son.js
Page({
   data;{
      ind_sonName:''
   },
   onLoad:function(){
   //先获取页面间通信通道对象
      const eventChannel = this.getOpenerEventChannel();
      eventChannel.on('indSonEvent',data=>{
         this.setData({
            ind_sonName:data
         })
      })
   }
})
  • 方法二中的eventChannel对象的emit()方法类似于vue中的$emit()方法,都是触发自定义事件,然后传数据;eventChannel.on()事件相当于vue中的$on()方法,监听eventChannel.emit()方法,在eventChannel.on()方法里,可以获取到当前页面传过来的数据。
  • 以上是今天学习的知识点总结,尤其是路由跳转传值是难以理解的,小伙伴们可以参考一下代码,有哪里不懂可以留言!!!