我正在尝试使用props作为规则编写if和else语句,并且不确定如何完全正确地构造它。 在路由器中,我有这个:
{
path: '/CardDisplay/:type',
name: 'CardDisplay',
props: true,
component: () => import('./views/CardDisplay.vue')
},
{
path: '/CardDisplay/find/:movie',
name: 'CardDisplay',
props: true,
component: () => import('./views/CardDisplay.vue')
},
然后我在另一个vue文件类型和电影中传递了两个道具。取决于调用,电影或类型将是不确定的,因此我想编写是否将围绕api fetch且当前页面如下的语句:
<template>
<div>
<movie-cards></movie-cards>
</div>
</template>
<script>
import MovieCards from '@/components/MovieCards';
import HomePlans from '@/components/HomePlans';
export default {
name: 'CardDisplay',
props:["type", "movie"],
components: {
MovieCards,
HomePlans
},
methods: {
getResults(props){
if(this.movie =="undefined"){
apiCall(isUpcoming){
const apiType = isUpcoming ? 'upcoming' : 'top_rated';
const url = `https://api.themoviedb.org/3/movie/${apiType}?api_key=api key&language=en-US&page=1`;
const response = await fetch(url);
let data = await response.json();
if (response.ok) {
this.results = data.results;
this.error = '';
} else {
this.results = [];
this.error = data.errors[0];
}
}
}else{
searchCall(searchTerm){
const urls = `https://api.themoviedb.org/3/search/movie?api_key=apikey&language=en-US&query=`;
const API_URL = `${urls}${this.searchTerm}`;
const responses = await fetch(API_URL);
let data = await responses.json();
if (responses.ok) {
this.results = data.results;
this.error = '';
} else {
this.results = [];
this.error = data.errors[0];
}
}
}
}
},
};
</script>
<style scoped>
</style>
但是,它使我在apiCall(isUpcoming){之后的if语句内出现错误,并强调了“ {”这句话;它不会编译。
我添加了包围所有内容的getResults,但不确定是否有必要。