英文:
Passing an object or parameter with @click
问题
我创建了一个包含多个<div>
的列表,每个<div>
都有一个@click
事件。
<div class="md:grid-cols-2 lg:grid-cols-1 col-gap-2 md:col-gap-8 md:row-gap-10" v-for="c in 4">
<Course @click="$router.push('/detail')"
title="The Course"
description="A Course"
lessons="1"
length="1 hr"
/>
</div>
v-for
将是一个对象列表,但目前只需要一个索引。我想将索引传递给路由器。我不确定这是否是最佳方法,但<Course>
基本上是一个<div>
按钮,当点击时跳转到/detail
。/detail
页面如下:
<template>
Course Details {{ props.id }}
</template>
<script setup lang="ts">
const props = defineProps(
[
'id',
]
)
</script>
英文:
I created a list of div's where each has a @click
<div class="md:grid-cols-2 lg:grid-cols-1 col-gap-2 md:col-gap-8 md:row-gap-10" v-for="c in 4">
<Course @click="$router.push('/detail')"
title="The Course"
description="A Course"
lessons="1"
length="1 hr"
/>
</div>
The v-for
will be a list of objects, but for now an index is enough. I want to pass an index into the router. I am not sure if this is the best way to do this but the <Course>
is more of less a div
button that when clicked goes to /detail
. Which has
<template>
Course Details {{ props.id }}
</template>
<script setup lang="ts">
const props = defineProps(
[
'id',
]
)
</script>
答案1
得分: 0
以下是您要的翻译内容:
Course 组件:
<script setup>
import { useRouter } from 'vue-router';
const props = defineProps(['course']);
const router = useRouter();
const showDetails = () => {
router.push({ path: '/details', params: { id: props.course.id } });
}
</script>
<template>
<div @click='showDetails' class="course">
课程 {{ props.course.title }}
</div>
</template>
<style>
.course {
padding: 1rem 2rem;
margin: 1rem;
border: 1px solid gray;
border-radius: 4px;
}
</style>
CourseList 组件:
<script setup>
import Course from './Course.vue'
const courses = [
{
id: 1,
title: 'one',
desc: '第一门课程',
lessons: 1,
duration: 1,
},
{
id: 2,
title: 'two',
desc: '第二门课程',
lessons: 5,
duration: 1,
},
{
id: 3,
title: 'three',
desc: '第三门课程',
lessons: 4,
duration: 3,
},
]
</script>
<template>
<Course v-for="course in courses" :key="course.id" :course="course">
</Course>
</template>
英文:
I would create a Course component, that you call from the parent (see CourseList below) and pass in a course object. In the Course component you can handle the click event to call the router and pass the id as a param.
Something like this:
Course component:
<script setup>
import { useRouter } from 'vue-router';
const props = defineProps(['course']);
const router = useRouter();
const showDetails = () => {
router.push({path: '/details', params: {id: props.course.id })
}
</script>
<template>
<div @click='showDetails' class="course">
Course {{ props.course.title }}
</div>
</template>
<style>
.course {
padding: 1rem 2rem;
margin: 1rem;
border: 1px solid gray;
border-radius: 4px;
}
</style>
CourseList component:
<script setup>
import Course from './Course.vue'
const courses = [
{
id: 1,
title: 'one',
desc: 'First course',
lessons: 1,
duration: 1,
},
{
id: 2,
title: 'two',
desc: '2nd course',
lessons: 5,
duration: 1,
},
{
id: 3,
title: 'three',
desc: '3rd course',
lessons: 4,
duration: 3,
},
]
</script>
<template>
<Course v-for='course in courses' :key="course.id" :course="course">
</Course>
</template>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论