博客
关于我
夜光 带你走进微信小程序研发(十)
阅读量:267 次
发布时间:2019-03-01

本文共 906 字,大约阅读时间需要 3 分钟。

微信小程序开发实用指南

路由机制

相对路径

index.js 文件中,事件处理函数 beginAnswerwx.navigateTo 路由的 url 使用的是相对路径。例如:

  • ../ 表示当前文件所在目录的上一级目录,即 index 文件夹所在目录。
  • ../test/test 表示 index 文件夹所在目录中的 test 文件夹中的 test 文件。

此外:

  • ./ 表示当前目录。
  • ../../ 表示当前文件所在的上上级目录,以此类推。

绝对路径

绝对路径采用格式为 /pages/test/test,直接指向目标文件的绝对路径。即使将 url 修改为绝对路径,也能实现页面跳转。


带参跳转

test.js 文件中,answerClickA 函数通过以下代码实现了当 index=20 时从 test 页面跳转至 result 页面,并携带参数 ABC

if (this.data.index == 20) {    wx.redirectTo({        url: '/pages/result/result?A=' + this.data.A + '&B=' + this.data.B + '&C=' + this.data.C    });}

result 页面的 js 文件中,通过生命周期函数获取页面参数。例如,在 App 生命周期函数中定义 options,即可获取 ABC 的值,并赋值给 result 页面的变量。

开发者可通过 console.log(options) 打印 options 的值,验证携带参数是否正确。


其他技术点

1. Math.random()

Math.random() 生成一个 [0,1) 之间的随机数。

Math.random() > 0.5 ? 1 : -1

用于随机返回 1-1

2. 三目运算符

return Math.random() > 0.5 ? 1 : -1

生成 [0,1) 之间的数,若大于 0.5,返回 1,否则返回 -1

3. sort()

sort() 方法用于对数组元素进行排序。

转载地址:http://wrbo.baihongyu.com/

你可能感兴趣的文章
process.env.VUE_APP_BASE_API 获取不到
查看>>
Process.run() 和 Process.start() 之间的区别
查看>>
Processes
查看>>
Processing通过编程实现艺术设计_实现艺术和现实的交互---数据设计分析002
查看>>
ProcessOnLoading
查看>>
SpringBoot中集成screw(螺丝钉)实现数据库表结构文档生成
查看>>
PROFINET 模拟器使用教程
查看>>
Program type already present: android.support.v4.widget.EdgeEffectCompat
查看>>
PyTorch中文版官方教程来啦(附下载)
查看>>
Progress Kemp LoadMaster 远程命令执行漏洞复现(CVE-2024-1212)
查看>>
Project configuration is not up-to-date with pom.xml. Run Maven->Update Project
查看>>
Project Euler 15 Lattice paths
查看>>
Project Euler 48 Self powers( 大数求余 )
查看>>
Project Euler Problem 12: Highly divisible triangular number
查看>>
ProjectEuler 2
查看>>
projection介绍及EPSG:4326和EPSG:3857的投射转换
查看>>
project打开文件时,显示无法识别此文件格式?
查看>>
Prometheus + Grafana on Kubernetes部署
查看>>
prometheus + grafana进行服务器资源监控
查看>>
Prometheus Alertmanager 告警配置详解
查看>>