划分组件中的vue-router参数
我有以下视图:
<template>
<div class="home">
<Quiz :quizID="this.$route.params.id"/>
<p>{{this.$route.params.id}}</p>
</div>
</template>
this。$ route.params.id
的值在&lt; p&gt;
标签之间正确显示。但是上面的组件不能将此值作为参数。如果我通过编写&lt; quiz:quizid =“ 3”/&gt;
来强制它,它可以显示与相应ID的组件QUIZ
。
但是,一旦我放了一个变量,就没有什么可用。
编辑:如建议,这是QUIZ
组件
<div>
<div class="container flex justify-center mx-auto">
<div class="flex flex-col">
<div class="w-full">
<div class="border-b border-gray-200 shadow">
<table>
<tbody class="bg-white">
<tr class="whitespace-nowrap" v-for="player in players" :key="player.quizID">
<td class="px-6 py-4 text-sm text-gray-500" v-if="player.quizID === quizID">
{{player.quizID}}
</td>
<td class="px-6 py-4" v-if="player.quizID === quizID">
{{player.name}}
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
name: 'Quiz',
data(){
return {
quizzes: [],
players: []
}
},
props: [
'quizID'
],
methods: {
async fetchQuizzes() {
const res = await fetch(`http://127.0.0.1:8000/api/quizs`)
const data = await res.json()
return data.data
},
async fetchPlayers() {
const res = await fetch(`http://127.0.0.1:8000/api/players`)
const data = await res.json()
return data.data
}
},
async created(){
this.quizzes = await this.fetchQuizzes();
this.players = await this.fetchPlayers();
},
}
</script>
有什么问题?
I have the following View :
<template>
<div class="home">
<Quiz :quizID="this.$route.params.id"/>
<p>{{this.$route.params.id}}</p>
</div>
</template>
The value of this.$route.params.id
is correctly displayed between the <p>
tags. But the component above doesn't get this value as a parameter. If I force it by writing <Quiz :quizID="3"/>
, it works and the component Quiz
with the corresponding ID is displayed.
But as soon I put a variable, nothing works anymore.
Edit : As suggested, here is the Quiz
component
<div>
<div class="container flex justify-center mx-auto">
<div class="flex flex-col">
<div class="w-full">
<div class="border-b border-gray-200 shadow">
<table>
<tbody class="bg-white">
<tr class="whitespace-nowrap" v-for="player in players" :key="player.quizID">
<td class="px-6 py-4 text-sm text-gray-500" v-if="player.quizID === quizID">
{{player.quizID}}
</td>
<td class="px-6 py-4" v-if="player.quizID === quizID">
{{player.name}}
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
name: 'Quiz',
data(){
return {
quizzes: [],
players: []
}
},
props: [
'quizID'
],
methods: {
async fetchQuizzes() {
const res = await fetch(`http://127.0.0.1:8000/api/quizs`)
const data = await res.json()
return data.data
},
async fetchPlayers() {
const res = await fetch(`http://127.0.0.1:8000/api/players`)
const data = await res.json()
return data.data
}
},
async created(){
this.quizzes = await this.fetchQuizzes();
this.players = await this.fetchPlayers();
},
}
</script>
What's wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论