通过 @click 传递一个对象或参数

huangapple go评论68阅读模式
英文:

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

        &lt;div class=&quot;md:grid-cols-2 lg:grid-cols-1 col-gap-2 md:col-gap-8 md:row-gap-10&quot; v-for=&quot;c in 4&quot;&gt;
            &lt;Course @click=&quot;$router.push(&#39;/detail&#39;)&quot;
                    title=&quot;The Course&quot;
                    description=&quot;A Course&quot;
                    lessons=&quot;1&quot;
                    length=&quot;1 hr&quot;
            /&gt;
        &lt;/div&gt;

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 &lt;Course&gt; is more of less a div button that when clicked goes to /detail. Which has

&lt;template&gt;
    Course Details {{ props.id }}
&lt;/template&gt;

&lt;script setup lang=&quot;ts&quot;&gt;

const props = defineProps(
    [
        &#39;id&#39;,
    ]
)

&lt;/script&gt;

答案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:

&lt;script setup&gt;
  import { useRouter } from &#39;vue-router&#39;;
  
  const props = defineProps([&#39;course&#39;]);
  const router = useRouter();
  
  const showDetails = () =&gt; { 
    router.push({path: &#39;/details&#39;, params: {id: props.course.id })
  }
  
&lt;/script&gt;

&lt;template&gt;
  &lt;div @click=&#39;showDetails&#39; class=&quot;course&quot;&gt; 
    Course {{ props.course.title }}
  &lt;/div&gt;
&lt;/template&gt;

&lt;style&gt;
  .course {
    padding: 1rem 2rem;
    margin: 1rem;
    border: 1px solid gray;
    border-radius: 4px;
  }
&lt;/style&gt;

CourseList component:

&lt;script setup&gt;
  import Course from &#39;./Course.vue&#39;

const courses = [
  {
    id: 1,
    title: &#39;one&#39;,
    desc: &#39;First course&#39;,
    lessons: 1,
    duration: 1,
  },
   {
    id: 2,
    title: &#39;two&#39;,
    desc: &#39;2nd course&#39;,
    lessons: 5,
    duration: 1,
  },
   {
    id: 3,
    title: &#39;three&#39;,
    desc: &#39;3rd course&#39;,
    lessons: 4,
    duration: 3,
  },
]  
 
&lt;/script&gt;

&lt;template&gt;
  &lt;Course v-for=&#39;course in courses&#39; :key=&quot;course.id&quot; :course=&quot;course&quot;&gt;
  &lt;/Course&gt;
&lt;/template&gt;

huangapple
  • 本文由 发表于 2023年5月30日 04:17:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76360114.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定