请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

为啥不在data中初始化year, month

我的index.js文件

import { getYear, getChineseMonth } from '../../../utils/helper'

Component({
  properties: {
    index: {
      type: Number,
      observer: function(newVal) {
        const val = newVal < 10 ? `0${newVal}` : newVal
        this.setData({
          _index: val
        })
      }
    }
  },

  data: {
    year: getYear(),
    month: getChineseMonth(),
    _index: 0
  }
})

老师的

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    index:{
      type:String,
      observer:function(newVal, oldVal, changedPath){
        let val = newVal < 10?'0'+newVal:newVal
        this.setData({
          _index:val
        })
      }
    }
  },
  // wxs

  /**
   * 组件的初始数据
   */
  data: {
    months: [
      '一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月',
      '十二月'
    ],
    year: 0,
    month: '',
    _index:''
  },

  attached:function(){
    let date = new Date()
    let year = date.getFullYear()
    let month = date.getMonth()

    this.setData({
      year,
      month:this.data.months[month]
    })
  }
})

正在回答 回答被采纳积分+3

1回答

7七月 2019-02-22 07:47:14

没太明白,您这个问题的具体意思是?

0 回复 有任何疑惑可以回复我~
  • 提问者 qiankunli #1
    我的意思是,直接在data中初始化year, month! 而不是在attached中再次setData。问题我已经修改了,老师可以看看代码对比!
    回复 有任何疑惑可以回复我~ 2019-02-22 09:42:42
  • 7七月 回复 提问者 qiankunli #2
    data是初始数据,只有attached时候才能知道真正的数据呀。
    回复 有任何疑惑可以回复我~ 2019-02-22 13:21:33
  • 提问者 qiankunli 回复 7七月 #3
    不管在什么地方拿都是一样的,因为是内部数据。为啥要在attached中拿呢?
    回复 有任何疑惑可以回复我~ 2019-02-23 11:23:05
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号