微信小程序 - 导航 、wxs及生命周期函数

导航
声明式导航

使用<navigator></navigator>标签

属性 类型 默认值 必填 说明
target string self 在哪个目标上发生跳转,默认当前小程序
url string 当前小程序内的跳转链接
open-type string navigate 跳转方式

target参数

属性值 说明
self 当前小程序
miniProgram 其它小程序

open-type参数

属性值 说明
navigate 保留当前页面,跳转到应用内的某个页面。但是不能跳到 tabbar 页面。
redirect 关闭当前页面,跳转到应用内的某个页面。但是不允许跳转到 tabbar 页面。
switchTab 跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面
reLaunch 关闭所有页面,打开到应用内的某个页面
navigateBack 关闭当前页面,返回上一页面或多级页面。可通过getCurrentPages获取当前的页面栈,决定需要返回几层
exit 退出小程序,target="miniProgram"时生效
<!-- navigate 保留当前页面,跳转到应用内的某个页面。但是不能跳到 tabbar 页面。 -->
<!-- switchTab 跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面 -->
<navigator url="../index/index" open-type="navigate">去非tabBar页面</navigator>

<!-- <navigator url="../mian/mian" open-type="switchTab">去tabBar页面</navigator> -->
编程式导航
<button type="primary" plain bind:tap="goMain">跳转到Main</button>

js中设置

 wx.navigateTo({
      url: '../mian/mian?id=1001&name=zhangsan',
      success: (res) => {
        console.log("跳转Main", res);
      }
    })
wxs

WXS(WeiXin Script)是内联在 WXML 中的脚本段。通过 WXS 可以在模版中内联少量处理脚本,丰富模板的数据预处理能力。

  1. 使用module.exports暴露方法(函数)
  2. module="m1" wxs设置名称
html 复制代码
<view>{{m1.toUpper(msg)}}</view>
<view>{{m2.toLower(msg)}}</view>
<wxs module="m1">
    module.exports.toUpper = function (str) {   
    return str.toUpperCase();
    }
</wxs>
<wxs module="m2" src="../../utils/tools.wxs"></wxs>
上拉触底、下拉刷新

1.开启下拉刷新、设置下拉触底的高度

2.在js文件中设置处理函数

javascript 复制代码
/**
   * 页面相关事件处理函数--监听用户下拉动作
   */
onPullDownRefresh() {
    console.log("下拉刷新");
    this.setData({
        colorList: this.getColor()
    })

    wx.stopPullDownRefresh();  //停止下拉刷新的动作

},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom() {
        console.log("上拉触底");
        this.setData({
            colorList: [...this.data.colorList, ...this.getColor()]
        })
    },
相关推荐
小胖墩有点瘦1 小时前
基于RNN+微信小程序+Flask的古诗词生成应用
微信小程序·小程序·flask
java1234_小锋2 小时前
[免费]微信小程序(图书馆)自习室座位预约管理系统(SpringBoot后端+Vue管理端)(高级版)【论文+源码+SQL脚本】
微信小程序·自习室·java毕业设计·自习室预约·小程序自习室
帅次3 小时前
Flutter:StatelessWidget vs StatefulWidget 深度解析
android·flutter·ios·小程序·swift·webview·android-studio
帅次6 小时前
Flutter Widget 体系结构解析
android·flutter·ios·小程序·xcode·web app·dalvik
郑州拽牛科技14 小时前
开发社交陪玩app小程序
大数据·微信小程序·小程序·系统架构·开源软件
源码姑娘18 小时前
基于Nodejs的火车订票小程序(源码+部署教程)
小程序·毕业设计
HerayChen19 小时前
uniapp vue3 微信小程序 uni.chooseLocation使用
微信小程序·小程序·uni-app·微信小程序地理位置
东月之神19 小时前
【0基础跟AI学软考高项】进度管理
职场和发展·微信小程序
然后就去远行吧20 小时前
小程序 wxml 语法 —— 36 wxml 语法 - setData() 修改数据
小程序
说私域21 小时前
基于定制开发开源AI智能名片S2B2C商城小程序的零售运营策略研究
人工智能·小程序·开源·零售