wxs使用

"本文介绍了微信小程序(WXML)中如何使用WXS(WeiXin Script)作为过滤器来处理数据。WXS不能直接与JS交互,但可以通过模块导入并在WXML中调用。示例展示了如何在WXML中引入WXS文件,以及如何使用WXS进行图片URL的替换和日期格式化。通过`module.exports`导出函数,使得WXS可以在WXML中被引用,如`{{m1.replaceURL(item)}
摘要由CSDN通过智能技术生成

只能用在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 得到时间戳
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在小程序(微信小程序)使用 `getBoundingClientRect` 方法可以获取某个组件的位置信息,包括宽度、高度、左边界、右边界、上边界和下边界等。你可以通过以下步骤来使用它: 1. 首先,获取到目标组件的选择器,可以通过 `id` 或 `class` 来选择。 2. 在需要使用的页面或组件的 JavaScript 文件使用 `wx.createSelectorQuery()` 方法创建一个选择器查询对象。 3. 调用选择器查询对象的 `select()` 或 `selectAll()` 方法,传入目标组件的选择器,以获取该组件的节点信息。 4. 调用节点信息对象的 `boundingClientRect()` 方法,以获取该组件的位置信息。 5. 在回调函数,可以通过参数获取到位置信息对象,包括宽度、高度、左边界、右边界、上边界和下边界等。 6. 根据需要,可以在回调函数对获取到的位置信息进行处理或使用。 以下是一个示例代码片段,展示了如何在小程序使用 `getBoundingClientRect` 方法: ```javascript // 在页面或组件的 JavaScript 文件 // 获取目标组件的位置信息 wx.createSelectorQuery().select('.target-component').boundingClientRect(function(rect) { // 获取到位置信息后的回调函数 console.log(rect.width); // 打印组件宽度 console.log(rect.height); // 打印组件高度 console.log(rect.left); // 打印组件左边界的位置 console.log(rect.right); // 打印组件右边界的位置 console.log(rect.top); // 打印组件上边界的位置 console.log(rect.bottom); // 打印组件下边界的位置 }).exec(); ``` 请注意,`getBoundingClientRect` 方法需要在调用 `exec()` 方法后才能生效,因为选择器查询是异步执行的。 希望以上信息能对你有所帮助!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值