【问题标题】:undefined (reading 'valueOf') finding the number of the week in Vueundefined (reading 'valueOf') 在 Vue 中查找周数
【发布时间】:2021-11-14 00:12:26
【问题描述】:

我正在尝试查找星期几。所以为此我的功能是:

getWeekNumber(date) {
            const temp_date = new Date(date.valueOf());
            const dayn = (date.getDay() + 6) % 7;
            temp_date.setDate(temp_date.getDate() - dayn + 3);
            const firstThursday = temp_date.valueOf();
            temp_date.setMonth(0, 1);
            if (temp_date.getDay() !== 4) {
                temp_date.setMonth(0, 1 + ((4 - temp_date.getDay() + 7) % 7));
            }
            return 1 + Math.ceil((firstThursday - temp_date) / 604800000);
        },

我正在其他功能中使用此功能来查找产品:

findProducts(products, date) {
            return products.filter((product) => {
                const formated_date = this.setDateFormat(product.attributes.date);
                console.log(formated_date);
                console.log(formated_date.valueOf());
                return (
                    formated_date.getDate() === date.getDate() &&
                    formated_date.getMonth() === date.getMonth() &&
                    formated_date.getFullYear() === date.getFullYear() &&
                    this.getWeekNumber(formated_date) === this.currentWeekNumber
                );
            });
        },

但我的问题是在控制台中的 findProducts 函数内部,我可以看到如下输出: 2021 年 9 月 28 日星期二 02:00:00 GMT+0200(中欧夏令时间) 表.vue:144 1632787200000

但是每当它转到 getWeekNumber 函数时,我都会收到此错误: vue.esm.js:1897 TypeError: Cannot read properties of undefined (reading 'valueOf')

你知道为什么会这样吗?

谢谢...

【问题讨论】:

    标签: vue.js date vuejs2 value-of


    【解决方案1】:

    自己实现与日期相关的概念绝不是一个好主意。有很多你不知道的警告。我建议使用类似Day.js

    特别是Week of Year

    【讨论】:

      【解决方案2】:

      也许这里的问题是作用域,lambdas删除“this”对象,例如:

      const test = {
        x : "hello",
        lambda: () => this.x,
        fun: function () { return this.x}
      }
      
      
      console.log(test.lambda()) //undefined
      console.log(test.fun()) //"hello"

      要解决这个问题,您可以将“this”对象包装在另一个对象中,例如:

      findProducts(products, date) {
                  let vueThis = this
                  return products.filter((product) => {
                      const formated_date = this.setDateFormat(product.attributes.date);
                      console.log(formated_date);
                      console.log(formated_date.valueOf());
                      return (
                          formated_date.getDate() === date.getDate() &&
                          formated_date.getMonth() === date.getMonth() &&
                          formated_date.getFullYear() === date.getFullYear() &&
                          vueThis.getWeekNumber(formated_date) === vueThis.currentWeekNumber
                      );
                  });
              },
      

      通过这种方法,您可以在 lambda 中使用来自 vue 的“this”上下文,您也可以将其更改为匿名函数并稍后调用它。

      【讨论】:

        猜你喜欢
        • 2022-08-10
        • 2023-02-16
        • 1970-01-01
        • 2023-02-11
        • 2021-11-03
        • 2021-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多