【发布时间】:2020-05-20 01:16:20
【问题描述】:
我正在使用 VueJs 构建一个 webapp。我有三个页面,应用程序,注册表,身份。在应用程序页面中,有一个表格,其中包含带有 link1 (localhost:8080/apps/{appid}/regs) 的应用程序,可在每一行中查看其注册表。同样在注册表页面中,有一个 link2 (localhost:8080/apps/{appid}/regs/{regid}/ids) 查看它的应用程序。
我的问题是,当我点击 link1 时,它会将我带到注册表页面,但是当我点击 link2 时,它会将我带到 localhost:8080 而不是 localhost:8080/apps/{appid}/regs/{regid}/ids。但是,当我从 link2 复制地址并粘贴到地址栏中时,它会将我带到所需的页面。我做错了什么?
我也在控制台中收到以下警告 -
[Vue 警告] 命名路由“regs”缺少参数:预期“appid”为 被定义
[Vue 警告] 命名路由“ids”缺少参数:预期“appid”为 定义
router.js
{
path: "apps",
name: "apps",
component: () => import("./components/protected/Apps"),
},
{
path: "apps/:appid/regs",
name: "regs",
props: true,
component: () => import("./components/protected/Regs"),
},
{
path: "apps/:appid/regs/:regid/ids",
name: "ids",
props: true,
component: () => import("./components/protected/Ids"),
},
Apps.vue
<template>
<table class="table table-striped mg-b-0">
<thead>
<tr>
<th class="tx-left w-5">#</th>
<th class="tx-left">Application</th>
<th class="tx-left text-center w-5">Action</th>
</tr>
</thead>
<tbody>
<tr v-bind:key="index" v-for="(app, index) in apps">
<td class="masked">{{index+1}}</td>
<td>{{app.name}}</td>
<td class="text-center">
<router-link class="bg-white" :to="{ name: 'regs', params: { appid: app.id}}">
<i class="fa fa-border fa-eye" title="View Registries"/>
</router-link>
</td>
</tr>
</tbody>
</table>
</template>
Regs.vue
<template>
<template v-if="appid">
<table class="table table-striped mg-b-0">
<thead>
<tr>
<th class="tx-left w-5">#</th>
<th class="tx-left">Registry</th>
<th class="tx-left text-center w-5">Action</th>
</tr>
</thead>
<tbody>
<tr v-bind:key="index" v-for="(reg, index) in regs">
<td class="masked">{{index+1}}</td>
<td>{{reg.name}}</td>
<td class="text-center">
<router-link class="bg-white" :to="{ name: 'ids', params: { appid: appid, regid: reg.id}}">
<i class="fa fa-border fa-eye" title="View Identities"/>
</router-link>
</td>
</tr>
</tbody>
</table>
</template>
</template>
<script>
export default {
name: "Regs",
props: ['appid'],
....
}
Ids.vue
<template>
<template v-if="appid && regid">
<table class="table table-striped mg-b-0">
<thead>
<tr>
<th class="tx-left w-5">#</th>
<th class="tx-left">Identity</th>
</tr>
</thead>
<tbody>
<tr v-bind:key="index" v-for="(id, index) in ids">
<td class="masked">{{index+1}}</td>
<td>{{id.name}}</td>
</tr>
</tbody>
</table>
</template>
</template>
<script>
export default {
name: "Ids",
props: ['appid','regid'],
....
}
</script>
【问题讨论】:
-
我猜你的变量
appid(通过属性分配)实际上没有值。相反,它应该是$router.params.appid -
是的,appid确实是路由器参数
-
是的。我的意思是您正在使用:
:to="{ name: 'ids', params: { appid: appid, regid: reg.id}}"。在这种情况下,appid将引用您的prop。但是,props没有被路由器补水,所以它应该是::to="{ name: 'ids', params: { appid: $router.params.appid, regid: reg.id}}" -
我正在使用router.vuejs.org/guide/essentials/passing-props.html 这个文档,我想我已经遵循了他们试图指示的内容。我还使用了您的建议(我认为应该是 $route 而不是 $router),仍然是同样的问题。单击链接将我登陆到 localhost:8080 但复制链接地址并将其粘贴到地址栏中似乎有效
-
请检查
Regs.vuefile。regs数据中是否有id属性。
标签: vue.js vue-router routerlink