小程序关键字变色实现

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
  线上项目:掌上海博

原文地址:https://www.cnblogs.com/starryqian/p/11883622.html