Vue路由器链接使用Vue Bootstrap表中的另一列数据路径

时间:2019-12-19 21:29:18

标签: javascript vue.js vue-router

我在vue引导程序中有包含用户数据的表。我也在使用Vue Router进入用户个人资料页面。个人资料页面路径为/user/idNumber,我想在“名称”下进行路由。像这样:

<a href="/users/idNumber"> Name </a>

我该怎么做?

代码:

<b-table striped hover :items="usersList" :fields="fields">
    <template v-slot:cell(_id)="data">
        <router-link :to="`/user/${data.value}`">{{ data.value }}</router-link>
    </template>
    <template v-slot:cell(fullname)="data">
        <router-link :to="`/user/${data.value}`">{{ data.value }}</router-link>
    </template>
</b-table>

usersList:

[{"_id":"5df9663acf06e2001742ac17","name":"Retdadada5155465","lastname":"Nienow","email":"Laverne55@yahoo.com","age":"1995","gender":"female","weight":58,"height":166,"activity":1.4,"value":3,"_createdOn":"2019-12-17T23:35:22.339Z","_updatedOn":"2019-12-19T20:57:30.588Z"},{"_id":"5df842f2cf06e2001742a8ec","name":"Retdadada516","lastname":"Nienow","email":"Laverne55@yahoo.com","age":"1993","gender":"female","weight":58,"height":166,"activity":1.4,"value":3,"_createdOn":"2019-12-17T02:52:34.183Z","_updatedOn":"2019-12-19T20:57:42.352Z"},{"_id":"5df7c972bca42200177decb4","name":"Lue","lastname":"Schneider","email":"Rosella93@yahoo.com","age":"1997","gender":"female","weight":60,"height":180,"activity":1.6,"_createdOn":"2019-12-16T18:14:10.554Z","_updatedOn":"2019-12-19T20:57:51.550Z"}]'

字段:

["_id",
                {
                    key: "fullname",
                    label: "Fullname",
                    sortable: true,
                    formatter: (value, key, item) => {
                        return item.name + " " + item.lastname
                    },
                },
                {
                    key: "birthYear",
                    label: "Age: ",
                    sortable: true,
                    formatter: (value, key, item) => {
                        return new Date().getFullYear() - item.age
                    },
                },
            ],

致谢

2 个答案:

答案 0 :(得分:1)

只需对第二个全名模板做些改动即可。如我所见,第一个应该没问题。

您只需使用data.item来利用循环项中的任何数据

<template>
  <b-table striped hover :items="usersList" :fields="fields">
    <template v-slot:cell(_id)="data">
      <router-link :to="`/user/${data.value}`">{{ data.value }}</router-link>
    </template>
    <template v-slot:cell(fullname)="data">
      <router-link :to="`/user/${data.item._id}`">{{ data.value }}</router-link>
    </template>
  </b-table>
</template>

工作沙箱:https://codesandbox.io/s/bootstrap-vue-sandbox-wvv9j

答案 1 :(得分:0)

如果您在此处查看文档:{​​{3}},您会发现使用范围域槽位时,data对象具有多个属性。其中之一是item,代表整个行的项目。因此,您可以改为:

<router-link :to="`/user/${data.item.someProperty}`">
    {{data.value}}
</router-link>

someProperty是要放置在路由路径中的用户对象的属性。