生成vue中的随机数据



我试图从通过api获得的数据生成随机报价。我生成了一个随机索引,但当我点击相应的按钮时,我没有得到一个随机报价。我没有得到任何明显的错误。

randomQuote() {
const index = Math.floor(Math.random() * this.quotes.length);
return this.quotes[index]
},

这是我的数据对象。

data: {
data: [],
quotes: [],
currentIndex: 0,
currentPage: 1,
},

这是我的axios调用:

axios.get(url).then(res => {
this.data = res.data;
this.quotes = this.data;
});

在您的axios调用中有一个明显的错误,您没有将quotes设置为正确的对象:

axios.get(url).then(res => {
this.quotes = res.data.quotes;
});

最新更新