【发布时间】:2017-08-30 18:17:03
【问题描述】:
我有一个从 RESTful API 获取 json 的代码。但它只显示 .container 并且它说 items 数组中没有任何内容。神秘的是它没有显示任何错误。所以我试图调试它显示使用console.log获取的结果,所以我在代码下添加了let result = await fetch('video').then(res => res.json()),但它在浏览器控制台上没有显示任何内容。好像它没有运行异步 getData 函数,但我不知道..
<template lang="pug">
.container
.columns(v-for="n in lines")
.column.is-3.vid(v-for='item in items')
.panel
p.is-marginless
a(:href='item.videoId')
img(:src='item.thumbnail')
.panel.vidInfo
.columns.hax-text-centered
.column
.panel-item.reddit-ups
span {{ item.score }}
i.fa.fa-reddit-alien.fa-2x
.panel-item.reddit-date
i.fa.fa-calendar.fa-2x
</template>
<script>
export default {
name: 'main',
data: () => ({
items: [],
lines: 0
}),
async getVideo () {
this.items = await fetch('/video').then(res => res.json())
this.lines = Math.ceil(this.items.length/4)
}
}
</script>
【问题讨论】:
标签: javascript asynchronous vue.js vuejs2 vue-component