划分组件中的vue-router参数

发布于 2025-01-22 16:21:58 字数 2169 浏览 0 评论 0原文

我有以下视图:

<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 Quizwith 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文