【问题标题】:VueJs router-link not working, but link copied from it works in another tabVueJs 路由器链接不起作用,但从中复制的链接在另一个选项卡中有效
【发布时间】: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


【解决方案1】:

您似乎已经在使用道具 appid' 和 'regid' ,其中道具尚未在组件本身中完全准备好。这就是 vue 返回警告的原因。

您可以在执行 vfor 之前先设置一个条件,以确保 props 已经有一个值。喜欢这个

Regs.vue

 <template v-if="appid">
       <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>
</template>

Ids.vue

<template v-if="appid &&  regid">
    <tr v-bind:key="index" v-for="(id, index) in ids">
            <td class="masked">{{index+1}}</td>
            <td>{{id.name}}</td>
    </tr>
</template>

【讨论】:

  • 也试过了,仍然收到警告。检查我的编辑
猜你喜欢
  • 2018-09-23
  • 2021-10-26
  • 1970-01-01
  • 2018-10-10
  • 1970-01-01
  • 2019-11-19
  • 2020-01-15
  • 2021-07-12
  • 2022-01-18
相关资源
最近更新 更多