微信小程序种子项目之wxs的使用

前言

在微信小程序中是没有办法像vue或者react那样在html中调用js中的方法,腾讯说不行就是不行,你说了算,哈哈哈

因此微信官方文档中给出了wxs的解决方法,其实本质上也是在.wxs文件中写javascript代码,没啥区别

如何定义

在untils创建format.wxs,后缀是.wxs,不是js

/**
 * 格式化播放量
 * @param count \
 */
function formatCount (count) {
  var count = parseInt(count)
  if (count > 100000000) {
    return (count / 100000000).toFixed(1) + '亿万'
  } else if (count > 10000) {
    return (count / 10000).toFixed(1) + '万'
  } else {
    return count + ""
  }
}
module.exports = {
  formatCount: formatCount
}

在wxs文件中是不支持ES6语法的哦

如何使用

在.wxml中引入并使用方法

# 引入
<wxs src="../../utils/format.wxs" module="format"></wxs>

# 使用
<view>{{ format(100000000) }}</view>

效果图

posted @ 2021-12-27 21:36  杨凌云的博客  阅读(408)  评论(0)    收藏  举报