如何访问 vue js 列表中的对象元素(nuxt)
how to access an object element in a vue js list (nuxt)
我有一个这样的对象数组:
{ id: 33617,
datePublication: 1532266465,
dateUpdate: 1532266574,
headline: 'An headline title here',
images: [ [Object] ] },
{ id: 33614,
datePublication: 1532265771,
dateUpdate: 1532271769,
headline: 'another super headline article',
images: [ [Object], [Object], [Object], [Object], [Object] ] }
所以我在我的 vue js 模板上以这种方式迭代:
<v-flex v-for="(ip, i) in ips" :key="i" xs12 sm6 >
<v-card>
<v-card-media
:src="ip.images[0].url"
height="200px"
/>
<v-card-title primary-title>
<div>
<h3 class="headline mb-0">{{ ip.headline }}</h3>
<div>Located two hours south of Sydney in the <br>Southern Highlands of New South Wales, ...</div>
</div>
</v-card-title>
<v-card-actions>
<v-btn flat color="orange">Share</v-btn>
<v-btn flat color="orange">Explore</v-btn>
</v-card-actions>
</v-card>
</v-flex>
这是我的关联脚本:
var api = "https://my api.org/news/fr-FR/2018-25-07";
export default {
data() {
return {};
},
layout: "no-live",
async asyncData({ app }) {
const ips = await app.$axios.$get(api);
console.log(ips);
return { ips };
}
};
图像数组中的每个对象都应该 return 我一个 id 和一个 url,所以我想把那个 url 作为我图像中的源,但是我有这个错误: 无法读取 属性 未定义的“0”
看来我需要对图像数组进行另一个循环,是否有使用 vue JS 的正确方法?
如果您在 ips
中有任何对象没有 images
,您将收到该错误,
可以尝试加个条件不渲染出错
<v-card-media
v-if="ip.images && ip.images[0]"
:src="ip.images[0].url"
height="200px"
/>
我通常会在这些情况下添加一个 <pre>{{ip}}</pre>
以查看发生了什么。
尝试将 v-if="ip.images" 放在 v-card-media 组件上。您将确保图片不为空且已加载。
我有一个这样的对象数组:
{ id: 33617,
datePublication: 1532266465,
dateUpdate: 1532266574,
headline: 'An headline title here',
images: [ [Object] ] },
{ id: 33614,
datePublication: 1532265771,
dateUpdate: 1532271769,
headline: 'another super headline article',
images: [ [Object], [Object], [Object], [Object], [Object] ] }
所以我在我的 vue js 模板上以这种方式迭代:
<v-flex v-for="(ip, i) in ips" :key="i" xs12 sm6 >
<v-card>
<v-card-media
:src="ip.images[0].url"
height="200px"
/>
<v-card-title primary-title>
<div>
<h3 class="headline mb-0">{{ ip.headline }}</h3>
<div>Located two hours south of Sydney in the <br>Southern Highlands of New South Wales, ...</div>
</div>
</v-card-title>
<v-card-actions>
<v-btn flat color="orange">Share</v-btn>
<v-btn flat color="orange">Explore</v-btn>
</v-card-actions>
</v-card>
</v-flex>
这是我的关联脚本:
var api = "https://my api.org/news/fr-FR/2018-25-07";
export default {
data() {
return {};
},
layout: "no-live",
async asyncData({ app }) {
const ips = await app.$axios.$get(api);
console.log(ips);
return { ips };
}
};
图像数组中的每个对象都应该 return 我一个 id 和一个 url,所以我想把那个 url 作为我图像中的源,但是我有这个错误: 无法读取 属性 未定义的“0”
看来我需要对图像数组进行另一个循环,是否有使用 vue JS 的正确方法?
如果您在 ips
中有任何对象没有 images
,您将收到该错误,
可以尝试加个条件不渲染出错
<v-card-media
v-if="ip.images && ip.images[0]"
:src="ip.images[0].url"
height="200px"
/>
我通常会在这些情况下添加一个 <pre>{{ip}}</pre>
以查看发生了什么。
尝试将 v-if="ip.images" 放在 v-card-media 组件上。您将确保图片不为空且已加载。