我面臨一個問題,即 owl carousel 中發生的事件不會更新函式之外的資料值
這是我的代碼:
endScenario: function() {
$(".owl-carousel").on('changed.owl.carousel', function(e) {
this.carouselIndex = e.item.index;
var numCarouselItems = $('.carousel__item').length - 1;
if (numCarouselItems === this.carouselIndex) {
this.isEndingActive = true
}
console.log(this.carouselIndex)
console.log(this.numCarouselItems)
console.log("this is inside the owl function : " this.isEndingActive)
});
console.log("this is outside the owl function : " this.isEndingActive)
}
data: {
isEndingActive: false,
}
用戶應該滾動輪播,當用戶處于輪播的最后一張幻燈片時,它將觸發 endScenario 函式,該函式假設更新 isEndingActive: false -> true 但當滿足要求時,它不會觸發從假到真的變化。是否有原因以及如何糾正此問題?

uj5u.com熱心網友回復:
似乎this的用法
外部this不等于內部this
你可以通過箭頭函式解決它
$(".owl-carousel").on('changed.owl.carousel', e => {}
或者你可以保存一個變數
endScenario: function() {
const self = this;
$(".owl-carousel").on('changed.owl.carousel', function(e){
//use self replace this
});
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/385870.html
