首页 > 技术文章 > 微信小程序在wxml中调用自定义函数

wujiaxing 2021-01-09 16:25 原文

微信小程序是不能直接在wxml调用自定义函数的

 

第一步: 新建 util.wxs 后缀的文件

 

 

第二步: 编写函数并导出

function domainPush(url) {
 if(url && (url.indexOf('https://') !== -1 || url.indexOf('http://') !== -1)) {
  return url;
 } else {
   return 'https://www.xhzsxx.net/' + url;
 }
}

module.exports = {
  domainPush: domainPush
}

 

第三步: 在wxml中引用文件

<wxs module="util" src="../../utils/wxParse.wxs"></wxs>

 

第四步:调用

<img src="{{util.domainPush(item.attr.src)}}" />

 

推荐阅读