2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
In Nuxt 3, usev-data-table
When we use a component, we want to redirect the page when we click a row or a cell. We can listen to the click event of the component and useuseRouter
To jump to the page.
- <template>
- <v-data-table
- :headers="headers"
- :items="items"
- @click:row="handleRowClick"
- ></v-data-table>
- </template>
-
- <script setup>
- import { useRouter } from 'vue-router';
-
- const router = useRouter();
-
- const headers = [
- { text: 'Name', value: 'name' },
- { text: 'Age', value: 'age' },
- // ...
- ];
-
- const items = [
- { name: 'John Doe1', age: 30, id: 1 },
- { name: 'Jane Smith2', age: 25, id: 2 },
- // ...
- ];
-
- // 处理行点击事件
- function handleRowClick(item) {
- // 使用item.id或其他标识符来确定要跳转到的具体页面
- router.push(`/user/${item.id}`);
- }
- </script>
In this example, when any row of the table is clicked,handleRowClick
The function will be called and useuseRouter
ofpush
method to jump to a new page, the path of which is dynamically generated by the user ID. You need to ensure that each user ID is unique in order to correctly jump to the corresponding detail page