【发布时间】:2020-07-16 05:45:21
【问题描述】:
我在 vue 中获取数组长度时遇到了一点问题。数组 在数据对象中为
data() {
return {
slides: [
{
image: require("@/assets/img/carousel/couple.jpg"),
caption:
"A couple wearing masks kiss in a shopping area in downtown Shanghai, China, on February 16, 2020."
},
{
image: require("@/assets/img/carousel/dogs.jpg"),
caption:
"Dogs wearing masks are seen in a shopping area in downtown Shanghai on February 16, 2020."
},
etc... ]
然后在方法中
methods: {
playSlides() {
this.imgSource = this.slides[this.currentSlide].image;
this.figCaption = this.slides[this.currentSlide].caption;
let slideInterval = setInterval(nextSlide, 2000);
function nextSlide() {
console.log(`slides.length is: ${this.slides.length}`);
this.currentSlide = (this.currentSlide + 1) % this.slides.length; // % is same as mod operator
console.log(Array.isArray(this.slides));
}
}
}
};
我知道这是漫长的一天,但 this.slides.length 未定义??并且 Array.isArray(this.slides) 为假。任何人都知道最受赞赏的问题是什么......
【问题讨论】:
-
const nextSlide = () => {...}绑定this。 -
对不起。仍然不确定。
-
您可能未定义,因为您在使用它的位置下方声明了变量 nextSlide。