string.wxs
/** * 参数1: 要截取的字符串 * 参数2: 开始截取的index * 参数3: 结束截取的index */ var sub = function (val, start, end) { if (val == undefined || val == '' || val == null) { return; } var reg = getRegExp('\\\n','g'); val = val.replace(reg,''); if (val.length > end) { return val.substring(start, end) + "..."; } else { return val; } } module.exports.sub = sub;
wxml文件引入:
<wxs src="../../wxs/string.wxs" module="tools" />
<navigator class="title" url="/pages/detail/detail?id={{item.id}}">{{tools.sub(item.title,0,22)}}</navigator>