已安装vuejs:无法设置未定义的值

我试图在挂载时运行以下功能,但出现错误

“无法设置属性“天”的未定义”

以下是我的代码

 function getDays(date) {
        this.days = (new Date()).getTime() / (1000 * 24 * 60 * 60);
        return parseInt(this.days) - this.START_DAYS;
  }

It is not helpful to tell which line is it. One of the reasons I did this.days is that VueJS had a problem for using let days

我对VueJS及其如何访问变量不太熟悉。

评论
  • 假装
    假装 回复

    You should create your getDays function in methods, then you can call it in mounted

    I don't know why did you use date arg in getDays, but it wasn't used it in getDays method

    ...
    data() {
      return {
        days: ''
        START_DAYS: ''
      }
    }
    methods: {
      getDays(date) {
        this.days = (new Date()).getTime() / (1000 * 24 * 60 * 60);
        return parseInt(this.days) - this.START_DAYS;
      }
    },
    mounted() {
      this.getDays()
    }
    ...
  • 不是本人
    不是本人 回复

    您是否以这种方式声明了您的方法? :

    methods: {
        getDays(date) {
          this.days = new Date().getTime() / (1000 * 24 * 60 * 60);
          return parseInt(this.days) - this.START_DAYS;
        }
      },
    

    Also, to call the method in mounted, use: this.getDays()

    Check this codesandbox for clarification