获取当前点击的组件信息:
e.target,如果需要组件存数据,可以指定data-NAME属性,然后通过基础事件中e.target.dataset.NAME 获取,如果是自定义事件在e.detail中获取
template组件,用来定义模板,用法:
数据:
date:{
user:{tit:'linge'}
}
编写模板组件:
<template name="student">
<text>{{tit}}<text>
</template>
使用模板:
<template is="student" data={{…user}}></template>
WXML提供两种文件引用方式import
和include:
import有作用域的概念,即只会import目标文件中定义的template,而不会import目标文件import的template,使用绝对路径。
include
可以将目标文件除了<template/>
的整个代码引入,相当于是拷贝到include
位置,目标文件的js和wxss分别需要单独require和@import到要使用的文件。
页面导航:
总结navigator 和 各种跳转API的区别:
wx.navigateTo 用于保留当前页面、跳转到应用内的某个页面,使用 wx.navigateBack可以返回到原页面。对于页面不是特别多的小程序,通常推荐使用 wx.navigateTo进行跳转, 以便返回原页面,以提高加载速度。当页面特别多时,则不推荐使用。
wx.redirectTo 当页面过多时,被保留页面会挤占微信分配给小程序的内存,或是达到微信所限制的 5 层页面栈。这时,我们应该考虑选择 wx.redirectTo。wx.redirectTo()用于关闭当前页面,跳转到应用内的某个页面。这样的跳转,可以避免跳转前页面占据运行内存,但返回时页面需要重新加载,增加了返回页面的显示时间。
wx.reLaunch 与 wx.redirectTo()的用途基本相同, 只是 wx.reLaunch()先关闭了内存中所有保留的页面,再跳转到目标页面。
wx.switchTab 对于跳转到 tab bar 的页面,最好选择 wx.switchTab(),它会先关闭所有非 tab bar 的页面。其次,也可以选择 wx.reLaunch(),它也能实现从非 tab bar 跳转到 tab bar,或在 tab bar 间跳转,效果等同 wx.switchTab()。使用其他跳转 API 来跳转到 tab bar,则会跳转失败。
wx.navigateBack 用于关闭当前页面,并返回上一页面或多级页面。
跳往tabbar页面的都不能在url中连接参数,可决办法:在app.js中定义globalData: {},用的时候,getApp().globalData
钩子函数接收参数:
onLauch() 小程序初始化 接受场景值
onShow() 场景值变化 接受新的场景值
onLoad() 接受页面导航url的传参
wxml插值表达式中使用函数:
//定义函数(可以写在view容器外,只能用es4语法)
<wxs module="func">
module.exports.match = function (str) {
var re = str.match(getRegExp('[\u4e00-\u9fa5]','g'))
if(!re) {
re = ''
}else{
var t = ''
for(var i = 0; i < re.length; i++){
if(re[i] != ',') {
t += re[i]
}
}
re = t
}
return re
}
</wxs>
//使用
<view class='item-row'>
<text>发证部门</text>
<text>{{func.match(item.issuingAuthority)}}</text>
</view>