【发布时间】:2020-12-12 23:20:00
【问题描述】:
我是 Vue 的新手,我正在使用 API 来显示数据,在我的代码中,当单击到类别时,它会保存该类别的名称并将用户重定向到另一个名为 category.vue 的页面,我的问题是在重定向用户之后我现在想显示仅收到的具有该类别名称的问题(作为过滤器),有没有办法做到这一点?
/* this is how i saved data to transfer to category page */
<div class="category" v-for="(categoryy,index) in category(question1)" :key="index">
<router-link :to="`/category/${category(question1)}`"> {{ categoryy }} </router-link>
</div>
category.vue
<template>
<div class="vw">
<p> related question of </p>
<p>{{ this.$route.params.cat }}</p> /* category name i sent appears here */
<ul class="container-question" v-for="(question1,index) in questions" :key="index"
>
/* THE PROBLEM : it shows all questions without filtering */
{{question1.question}}
</ul>
</div>
</template>
<script>
export default {
props:
{
question1: Object
},
data(){
return{
questions: []
}
},
computed:{
category(){
let category = this.$route.params.cat;
return category
}
},
mounted: function(){
fetch('https://opentdb.com/api.php?amount=10&category=9&difficulty=medium&type=multiple',{
method: 'get'
})
.then((response) => {
return response.json()
})
.then((jsonData) => {
this.questions = jsonData.results
})
}
}
</script>
【问题讨论】:
标签: javascript html vue.js