本文共 910 字,大约阅读时间需要 3 分钟。
在 index.js 文件中,事件处理函数 beginAnswer 的 wx.navigateTo 路由的 url 使用的是相对路径。例如:
../ 表示当前文件所在目录的上一级目录,即 index 文件夹所在目录。../test/test 表示 index 文件夹所在目录中的 test 文件夹中的 test 文件。此外:
./ 表示当前目录。../../ 表示当前文件所在的上上级目录,以此类推。绝对路径采用格式为 /pages/test/test,直接指向目标文件的绝对路径。即使将 url 修改为绝对路径,也能实现页面跳转。
在 test.js 文件中,answerClickA 函数通过以下代码实现了当 index=20 时从 test 页面跳转至 result 页面,并携带参数 A、B 和 C:
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,即可获取 A、B 和 C 的值,并赋值给 result 页面的变量。
开发者可通过 console.log(options) 打印 options 的值,验证携带参数是否正确。
Math.random()Math.random() 生成一个 [0,1) 之间的随机数。
Math.random() > 0.5 ? 1 : -1
用于随机返回 1 或 -1。
return Math.random() > 0.5 ? 1 : -1
生成 [0,1) 之间的数,若大于 0.5,返回 1,否则返回 -1。
sort()sort() 方法用于对数组元素进行排序。
转载地址:http://wrbo.baihongyu.com/