一次在写原生微信小程序的使用,突然在在wxml行内使用includes,如果直接使用是不起作用的
如果要写的话可以换一种方式去实现
就是用wxs
首先创建一个wxs文件
创建完在里面定义方法
主要wsx里面也有他的规范,不是所以js都支持
声明变量只允许使用val,wxs数组里面是没有includes方法的,要实现的话可以利用其他方法来实现
以下是wxs支持的数组方法
由于业务逻辑比较简单,我只用了for循环和forEach
var includes = function (arrays, searchElement) {
var isok = false
if (arrays.length) {
arrays.forEach(function (item) {
if (item == searchElement) {
isok = true
}
})
return isok
// for (var i = 0; i < arrays.length; i++) {
// if (arrays[i] == searchElement) {
// isok = true
// }
// }
// return isok
}
}
module.exports = {
includes: includes
}
然后在wxml中使用