lengziyu / noteBook

This is my noteBook.
0 stars 0 forks source link

wepy开发小程序注意事项 #25

Open lengziyu opened 5 years ago

lengziyu commented 5 years ago

在template使用类似vue的filter功能,在小程序template里无法调用methods里面的函数,无法使用定义的全局函数,例如在onLoad()里可以使用this.isArray()调用函数,在template却无法调用。

只能用wxs,WePY 从1.7.x 版本开始支持 wxs 语法。

mywxs.wxs

module.exports = {
  text: 'This is from wxs',
  filter: function (num) {
    return num.toFixed(2);
  }
};

index.wpy

<template>
  <text>{{m1.text}}</text>
  <text>{{m1.filter(num)}}</text>
</template>

<script>
  import wepy from 'wepy';
// 只能相对路径
  import mywxs from '../wxs/mywxs.wxs';

  export default class Index extends wepy.page {

    data = {
      num: 10
    };

    wxs = {
      m1: mywxs
    }

  };
</script>