PrimeVue DataTable列如何添加带参数的动态router-link链接
错误原因
你的代码不生效是两个核心问题:
- PrimeVue 13.x的Column组件自定义单元格内容,必须显式写
<template #default="slotProps">声明作用域插槽,直接在Column标签里写内容根本拿不到行数据,slotProps会是undefined - 路由参数里的id你加了单引号,写成了
'slotProps.data.id',这是个固定字符串,根本不会解析成变量值,路由跳转的时候会把这段文本当id传
可直接运行的正确代码
<DataTable :value="clients" responsiveLayout="scroll"> <Column field="name" header="Name"> <template #default="slotProps"> <router-link :to="{ name: 'client_profile', params: { id: slotProps.data.id }}"> {{ slotProps.data.name }} </router-link> </template> </Column> <Column field="notes" header="Notes"></Column> </DataTable>
备注
- 插槽拿到的
slotProps.data就是当前行对应的完整client对象,和你之前v-for里拿到的单个c对象完全一致,里面包含id、name、notes所有字段 - 所有需要自定义渲染的列,都套一层
<template #default="slotProps">就能拿到行数据做自定义处理,不要直接把内容写在Column标签里
内容的提问来源于stack exchange,提问作者ccsv
相关产品推荐
相关产品推荐

