我正試圖找到一周的數字。所以我的函式是:
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);
如果(temp_date.getDay() !==4) {
temp_date.setMonth(0, 1 ((4 - temp_date.getDay() 7) % 7)) 。
}
回傳1 Math.ceil((firstThursday - temp_date) / 604800000)。
},
我在我的其他函式中使用這個函式來尋找產品:
findProducts(products, date) {
return products.filter((product) => {
const formated_date = this.setDateFormat(product.attributions.date);
console.log(formated_date)。
console.log(formated_date.valueOf())。
回傳 (
formated_date.getDate() === date.getDate() &&
formated_date.getMonth() === date.getMonth() &&
formated_date.getFullYear() === date.getFullYear() &&
this.getWeekNumber(formated_date) === this.currentWeekNumber
);
});
},
但是我的問題是,在控制臺的findProducts函式里面,我可以看到這樣的輸出。 Tue Sep 28 2021 02:00:00 GMT 0200 (Central European Summer Time) Table.vue:144 1632787200000
但是每當它進入getWeekNumber函式時,我就會得到這個錯誤。 vue.esm.js:1897 TypeError: 不能讀取未定義的屬性(讀取'valueOf')
你知道為什么會這樣嗎?
。謝謝...
uj5u.com熱心網友回復:
自己實施與日期有關的概念永遠不是一個好主意。有很多你不知道的注意事項。我建議使用像Day.js這樣的東西。
特別是Week of Year
uj5u.com熱心網友回復:
也許這里的問題是作用域,lambdas洗掉了 "this "物件,例如:
。span class="hljs-keyword">const test = {
x : "hello"。
lambda。() => this.x。
fun: function () { return this.x}.
}
console.log(test.lambda() //undefined[/span]。
console.log(test.fun() //"hello"
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
為了解決這個問題,你可以把你的 "this "物件包在另一個物件里面,例如:
findProducts(products, date) {
let vueThis = this
return products.filter((product) => {
const formated_date = this.setDateFormat(product.attributions.date);
console.log(formated_date)。
console.log(formated_date.valueOf())。
回傳 (
formated_date.getDate() === date.getDate() &&
formated_date.getMonth() === date.getMonth() &&
formated_date.getFullYear() === date.getFullYear() &&
vueThis.getWeekNumber(formated_date) === vueThis.currentWeekNumber
);
});
},
通過這種方法,你在lambda內使用了vue的 "this "背景關系,你也可以將其改為匿名函式,然后再呼叫它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/327390.html
標籤:
上一篇:如何對動態道具進行條件渲染
下一篇:Vue獲取資料時,查詢引數不作業
