如何使用数组类型属性的元素初始化 Vue.js 属性



我需要使用变体数组的第一个元素的相应字段初始化属性:图像、描述、视频和标题。 我得到发送返回 json 文件的 ajax 请求的变体数组。

var app = new Vue({
el: '#app',
data: {
name: '<?php echo $name ?>',
image: '',
description: '',
video: '',
title: '',
variants: ''
},
methods: {
updateAll: function(im, des, t, v) {
this.image = im;
this.description = des;
this.title = t;
this.video = v;
},
getQuery: function() {
axios.get('ajaxfile.php', {
params: {
name: this.name
}
}).then(function(response) {
app.variants = response.data;
}).catch(function(error) {
console.log(error);
});
}
},
created: function() {
this.getQuery()
}
});

看起来您没有处理存储在app.variants中的 json,也没有调用updateAll()方法。假设app.variants不需要任何进一步的处理,那么当您通过 ajax 请求接收数据时调用updateAll()可能是最好的解决方案。

在这种情况下,我将简单地使用您拥有的updateAll((方法。 像这样:

}).then(function(response) {
app.variants = response.data;
this.updateAll(app.variants[0].image, app.variants[0].description, app.variants[0].title, app.variants[0].video)
}).catch(function(error) {

但是..这不是那么干净的:)

最新更新