vue组件数据和计算属性

Mat*_*inz 1 javascript vue.js vue-component vuejs2

我有以下问题.

我正在声明数据,我想在之前,当前和之后设置.

data () {
  return {
    notes: {
      ...
    },
    view: {
      dayCurrent: new Date(),
      dayBefore: new Date().setDate(this.view.dayCurrent.getDate() - 1),
      dayAfter: new Date().setDate(this.view.dayCurrent.getDate() + 1)
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

但是一旦我保存了这个,我就会得到以下错误

data()中的错误:"TypeError:无法读取未定义的属性'dayCurrent'",就好像我的视图对象不存在一样.

为什么是这样?如何设置此并避免此类错误.

Ana*_*dac 5

另一种解决方案是在声明dayCurrent之外return声明并在其他属性中引用它.

data () {
  const dayCurrent = new Date();
  return {
    notes: {
      ...
    },
    view: {
      dayCurrent: dayCurrent,
      dayBefore: new Date().setDate(dayCurrent.getDate() - 1),
      dayAfter: new Date().setDate(dayCurrent.getDate() + 1)
    }
  }
}
Run Code Online (Sandbox Code Playgroud)