微信小程序 在wxml中 调用不支持的方法

微信小程序 在wxml中 调用不支持的方法

Demo地址:https://download.csdn.net/download/qq_25992675/11453516

例:在wxml中使用indexOf
使用步骤
  1. 创建一个filter.wxs
  2. 在要适用的页面引入filter.wxs
  3. 使用方法
1、创建一个filter.wxs
/*
 * 需要使用的方法
 */
var indexOf = function (array,index) {
    return array.indexOf(index)
}

/*
 * 导出方法:左边的filter.wxs中的方法名,右边是页面引入的方法名
 */
module.exports = {
    indexOf: indexOf
}
2、在要适用的页面引入filter.wxs
<wxs module="filters" src="../../utils/filter.wxs"></wxs>
3、在要适用的页面使用方法(加粗部分是调用方法)
<view class="container">
    <view wx:for="{
   
   {list}}" wx:key="{
   
   {index}}">
        <text class="{
   
   {(filter.indexOf(otherList,item))>-1?'changColor':''}}">{
   
   {item}}</text>
    </view>
</view>
js部分代码
//获取应用实例
const app = getApp()

Page({
    data: {
        list: [1, 2, 3, 4, 5],
        otherList: [1, 3, 5]
    },
    onLoad: function() {}
})

猜你喜欢

转载自blog.csdn.net/qq_25992675/article/details/97788944