我是 Vue 的新手,现在我被困住了。对于练习,我正在为系列剧集 list 制作一个应用程序。该应用程序的第一部分搜索系列并将其中一个添加到数据库中。搜索结果给我这样的结果:/image/QuOfc.png
这是我的带有模板和脚本的代码:
<template>
<div class="series">
<ul>
<li v-for="item in series" :key="item.id">
<img :src="image_url+item.poster_path"/>
<div class="info">
{{item.name}}
<br/>
<h5>{{item.id}}</h5>
Start Date: {{item.first_air_date}}
<br/>
{{getEpisodeNumber(item.id)}}
<br/>
{{getSeasonNumber(item.id)}}
</div>
</li>
</ul>
</div>
</template>
<script>
export default {
name: "series",
props: ["series"],
data() {
return {
image_url: "https://image.tmdb.org/t/p/w500",
api_key: {-api key-},
episode_url: "https://api.themoviedb.org/3/tv/",
}
},
methods: {
async getEpisodeNumber(showID) {
const json = await fetch(this.episode_url + showID + this.api_key)
.then((res) => { return res.json() })
.then((res) => { return res.number_of_episodes })
return await json
},
async getSeasonNumber(showID) {
const json = await fetch(this.episode_url + showID + this.api_key)
.then((res) => { return res.json() })
.then((res) => { return res.number_of_seasons })
return await json;
}
},
}
</script>
方法应该返回一个数字,但它们返回一个对象,可能是 promise 对象。但是当我尝试 console.log 方法中的数据时,它们会打印一个值(int)。我需要达到这个值,但我被卡住了。我尝试过思考,但每次都失败了。
最佳答案
我刚刚创建了一个名为 show
的新组件,并将 item.id
传递给该组件。在 show
组件中,我使用另一个 fetch()
再次获取显示数据,现在它可以正常工作了。
关于javascript - Vuejs set方法返回模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51181938/