vant 自定义日期文案 怎么动态的传值进去?
发布于 4 年前 作者 pingzeng 11401 次浏览 来自 问答
这里面的值 formatter 应该 怎么动态的 传入进去? 有大佬知道吗?
Page({
  data: {
    formatter(day) {
      const month = day.date.getMonth() + 1;
      const date = day.date.getDate();

      if (month === 5) {
        if (date === 1) {
          day.topInfo = '劳动节';
        } else if (date === 4) {
          day.topInfo = '五四青年节';
        } else if (date === 11) {
          day.text = '今天';
        }
      }

      if (day.type === 'start') {
        day.bottomInfo = '入住';
      } else if (day.type === 'end') {
        day.bottomInfo = '离店';
      }

      return day;
    },
  },
});

1 回复

这里面的值 formatter 应该 怎么动态的 传入进去?

说实话,你这句话就算有配图和代码,我还是不明白你在问什么?

如果你是想问formatter(day)这个函数的执行结果怎么反映到页面上的话。请参考下面的代码。

以后提问题请尽量说清楚,大家都节省时间。

另外,你的代码的写法有点奇怪, 为啥将函数formatter(day)直接定义到了 Page.data 下,多读下文档,了解下这个data是做什么用的。

js:

Page({
  data: {
    formatStr: null
  },

 onLoad: function() {
    let that = this
    let day = xxx
    this.setData({
      formatStr: that.formatter(day)
    })
 },

 formatter: function(day) {
   ...
   return day
 }
})

wxml:

<view>{{formatStr}}</view>
回到顶部