【微信小程序-原生开发】wxml 支持 includes (wxml中执行函数的方法)

默认在 wxml 的 { {}}中,无法执行 includes ,可通过 wxs 实现,范例如下:

wxml 中

引入指定的 wxs ,module属性为模块名,src 属性为目标函数的地址

<wxs module="jstools" src="../../utils/wxs/includes.wxs"></wxs>

实现功能:当用户的角色列表中包含 “管理员” ,则可看到新人审核页面

<t-cell title="新人审核" wx:if="{
     
     { jstools.includes(userInfo.roleList,'管理员')}}" arrow url='/pages/me/friend/new/index' />

utils\wxs\includes.wxs

var includes = function (array, searchElement) {
    
    
  return array.indexOf(searchElement) !== -1
}
module.exports = {
    
    
  includes: includes
}

若有更复杂的函数需要在wxml 中使用,可参考此方式实现。

猜你喜欢

转载自blog.csdn.net/weixin_41192489/article/details/129407880
今日推荐