1.小程序中不支持渲染indexOf使用,我們可以通過新建wps來實現
function fn(arr, arg) { var result = { indexOf: false, toString: '' } result.indexOf = arr.indexOf(arg) > -1; result.toString = arr.join(","); return result; } module.exports.fn = fn;
2.index.wxml
<wxs src="../indexOf.wxs" module="tools" /> <!-- tools可以自定義 --> <view class="intro" > <block wx:for="{{keywords}}"> <text class=" {{tools.fn(ckey, item).indexOf?'active':'black'}}">{{item}}</text> </block> </view>
引入我們的wxs,module可以自己定義,下面模板渲染中可以使用
3.index.js
const app = getApp() Page({ data: { keywords:['我','愛','你'], ckey:['我','愛'] }, onLoad: function () { console.log('代碼片段是一種迷你、可分享的小程序或小游戲項目,可用於分享小程序和小游戲的開發經驗、展示組件和 API 的使用、復現開發問題和 Bug 等。可點擊以下鏈接查看代碼片段的詳細文檔:') console.log('https://mp.weixin.qq.com/debug/wxadoc/dev/devtools/devtools.html') }, })
小程序代碼塊鏈接:https://developers.weixin.qq.com/s/vmdBhXmA7acA
線上項目:掌上海博