WXS是一套运行在视图层的脚本语言???微信端的规范详见
它的特点是运行在视图层。
当需要避免逻辑层和渲染层交互通信折损时,可采用wxs。
uni-app可以将wxs代码编译到微信小程序、QQ小程序、app-vue、H5上(uni-app 2.2.5及以上版本)
与wxs类似,百度小程序提供了Filter、阿里小程序提供了SJS,uni-app也支持使用这些功能,并将它们编译到百度和阿里的小程序端。不过它们的功能还不如wxs强大。此外头条系小程序自身不支持类似功能。

平台差异说明

App H5 微信小程序 支付宝小程序 百度小程序 字节跳动小程序 QQ小程序
√(不支持nvue) SJS Filter x

App端nvue解决此类需求,不应该使用wxs,而是使用bindingx。

wxs示例

以下是一些使用 WXS 的简单示例,要完整了解 WXS 语法,请参考WXS 语法参考
本示例使用wxs响应touchmove事件,减少视图层与逻辑层通信,使滑动更加丝滑。

  1. <template>
  2. <view>
  3. <view class="area">
  4. <view @touchstart="test.touchstart" @touchmove="test.touchmove" class="movable">{{test.msg}}</view>
  5. </view>
  6. </view>
  7. </template>
  8. <script module="test" lang="wxs">
  9. var startX = 0
  10. var startY = 0
  11. var lastLeft = 50; var lastTop = 50
  12. function touchstart(event, ins) {
  13. console.log("touchstart")
  14. var touch = event.touches[0] || event.changedTouches[0]
  15. startX = touch.pageX
  16. startY = touch.pageY
  17. }
  18. function touchmove(event, ins) {
  19. var touch = event.touches[0] || event.changedTouches[0]
  20. var pageX = touch.pageX
  21. var pageY = touch.pageY
  22. var left = pageX - startX + lastLeft
  23. var top = pageY - startY + lastTop
  24. startX = pageX
  25. startY = pageY
  26. lastLeft = left
  27. lastTop = top
  28. ins.selectComponent('.movable').setStyle({
  29. left: left + 'px',
  30. top: top + 'px'
  31. })
  32. return false
  33. }
  34. module.exports = {
  35. msg: 'Hello',
  36. touchstart: touchstart,
  37. touchmove: touchmove
  38. }
  39. </script>
  40. <script>
  41. export default {
  42. data() {
  43. return {
  44. }
  45. },
  46. methods: {
  47. }
  48. }
  49. </script>
  50. <style>
  51. .area{
  52. position: absolute;
  53. width: 100%;
  54. height: 100%;
  55. }
  56. .movable{
  57. position: absolute;
  58. width: 100px;
  59. height: 100px;
  60. left: 50px;
  61. top: 50px;
  62. color: white;
  63. text-align: center;
  64. line-height: 100px;
  65. background-color: red;
  66. }
  67. </style>

支付宝小程序,百度小程序官方暂未支持事件响应,不过也可以使用对应的SJS、Filter过滤器实现一些数据处理的操作,以下代码展示了一个时间格式化的小功能
index.vue

  1. <template>
  2. <view>
  3. <view>
  4. {{timestr}} 是
  5. </view>
  6. <view>
  7. {{utils.friendlyDate(timestamp)}}
  8. </view>
  9. </view>
  10. </template>
  11. <script module="utils" lang="filter" src="./utils.filter.js"></script>
  12. <script module="utils" lang="sjs" src="./utils.sjs"></script>
  13. <script>
  14. export default {
  15. data() {
  16. return {
  17. timestr: '2019/08/22 10:10:10',
  18. timestamp: 0
  19. }
  20. },
  21. created() {
  22. this.timestamp = new Date(this.timestr).getTime()
  23. },
  24. methods: {
  25. }
  26. }
  27. </script>

utils.sjsutils.filter.js 内容一致

  1. export default {
  2. friendlyDate: (timestamp) => {
  3. var formats = {
  4. 'year': '%n% 年前',
  5. 'month': '%n% 月前',
  6. 'day': '%n% 天前',
  7. 'hour': '%n% 小时前',
  8. 'minute': '%n% 分钟前',
  9. 'second': '%n% 秒前',
  10. };
  11. var now = Date.now();
  12. var seconds = Math.floor((now - parseInt(timestamp)) / 1000);
  13. var minutes = Math.floor(seconds / 60);
  14. var hours = Math.floor(minutes / 60);
  15. var days = Math.floor(hours / 24);
  16. var months = Math.floor(days / 30);
  17. var years = Math.floor(months / 12);
  18. var diffType = '';
  19. var diffValue = 0;
  20. if (years > 0) {
  21. diffType = 'year';
  22. diffValue = years;
  23. } else {
  24. if (months > 0) {
  25. diffType = 'month';
  26. diffValue = months;
  27. } else {
  28. if (days > 0) {
  29. diffType = 'day';
  30. diffValue = days;
  31. } else {
  32. if (hours > 0) {
  33. diffType = 'hour';
  34. diffValue = hours;
  35. } else {
  36. if (minutes > 0) {
  37. diffType = 'minute';
  38. diffValue = minutes;
  39. } else {
  40. diffType = 'second';
  41. diffValue = seconds === 0 ? (seconds = 1) : seconds;
  42. }
  43. }
  44. }
  45. }
  46. }
  47. return formats[diffType].replace('%n%', diffValue);
  48. }
  49. }

注意

引入方式

  1. <!-- 内联 -->
  2. <script module="test" lang="wxs">
  3. //...code
  4. </script>
  5. <script module="utils" lang="filter">
  6. //...code
  7. </script>
  8. <!-- 外部引入 -->
  9. <script module="utils" lang="wxs" src="./utils.wxs"></script>
  10. <script module="utils" lang="filter" src="./utils.filter.js"></script>
  11. <script module="utils" lang="sjs" src="./utils.sjs"></script>
  • 【重要】** 编写wxs、sjs、filter.js 内容时必须遵循相应语法规范**
  • 【重要】 module所指定的模块名不可与datamethodscomputed内的属性重名
  • 目前各个小程序正在完善相关规范,可能会有较大改动,请务必仔细阅读相应平台的文档
  • 支付宝小程序请使用sjs规范,详见
  • 支付宝小程序sjs只能定义在.sjs 文件中,然后使用<script>标签引入
  • 支付宝小程序script的标签属性namefrom被统一为了modulesrc以便后续实现多平台统一写法
  • 百度小程序中请使用Filter规范,详见
  • 百度小程序Filter只能导出function函数
  • 暂不支持在 wxs、sjs、filter.js 中调用其他同类型文件
  • wxs、filter.js既能内联使用又可以外部引入,sjs只能外部引入
  • QQ小程序目前对内联的 wxs 支持不好,部分写法可能会导致编译出错,尽量使用外部引入的方式
  • 在微信自定义组件中wxcomponents也可以使用wxs
  • nvue页面暂不支持wxs、sjs、filter.js
  • 各个script标签会分别被打包至对应支持平台,不需要额外写条件编译
  • HBuilderX 2.2.5开始,不推荐使用各个小程序自有的引入方式,推荐使用script标签引入
  • App和H5端,提供了wxs的升级版,更加强大,见下面的 renderjs 章节