wxs使用

    科技2025-04-26  5

    只能用在wxml中,不能和js互相调用 暂时不支持es6

    在wxml中需要使用的使用,必须通过 wxs 标签导入,并且一定要给它取一个模块名称,后面就可以通过模块名称.的方式调用,可以充当过滤器的角色

    在页面中定义及使用:

    <view wx:key="item.id" class="comment" wx:for="{{shop.comments}}"> <text class="name">{{item.name}}</text> <text class="date">{{item.date}}</text> <text class="rating">{{item.rating}}</text> <text>{{item.content}}</text> <view class="images"> <image wx:key="*this" wx:for="{{item.images}}" src="{{m1.replaceURL(item)}}"></image> </view> </view> <wxs module='m1'> function replaceURL(originURL){ return originURL.replace('w.h','200.200') } module.exports = { replaceURL:replaceURL } </wxs>

    单独抽出来定义及使用:

    在utils中定义format.wxs: function formatTime (d) { var date = getDate(d) var year = date.getFullYear() var month = date.getMonth() + 1 var day = date.getDate() var hour = date.getHours() var minute = date.getMinutes() var second = date.getSeconds() // return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':') return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute].map(formatNumber).join(':') } function formatNumber (n) { n = n.toString() return n[1] ? n : '0' + n } module.exports = { formatTime: formatTime } 在使用的wxml中引入并使用: <wxs src="../../utils/format.wxs" module="format" /> <text class="date">{{format.formatTime(setting.date)}}</text> new Date() - 0 得到时间戳
    Processed: 0.009, SQL: 8