vuejs Navbar链接不起作用

时间:2019-03-06 15:19:08

标签: vue.js vuejs2 vue-component vuex vue-router

我的vuejs应用程序具有导航栏,其代码如下。 问题是导航栏显示有其颜色,但导航栏上没有可用的链接。控制台也会显示类似click here to view the error的错误。当我搜索stackoverflow时,我发现一些与我的问题有关的答案正在将 auth:更改为 auth:{} 。但是它也起作用了。所以有什么可以帮我解决的。谢谢

<template>
<nav class="navbar navbar-expan-lg navbar-dar bg-primary rounded">
<button class="navbar-toggler"
type="button"
data-toggle="collapse"
data-target="#navbar1"
aria-controls="navbar1"
aria-expanded="false"
aria-label="Toggle navigation">
  <span class="navbar-toggler-icon"></span>
</button>

<div class="collapse navbar-collapse justify-content-md-center"    id="navbar1">
  <ul class="navbar-ul">
    <li class="nav-item">
      <router-link class="nav-link" to="/home">Home</router-link>
    </li>
     <li v-if="auth == ''" class="nav-item">
      <router-link class="nav-link" to="/login">Login</router-link>
    </li>
     <li v-if="auth == ''" class="nav-item">
      <router-link class="nav-link" to="/register">Register</router-link>
    </li>
     <li v-if="auth == 'loggedin'" class="nav-item">
      <router-link class="nav-link" to="/profile">Profile</router-link>
    </li>
     <li v-if="auth == 'loggedin'" class="nav-item">
      <router-link class="nav-link" to="/logout">Logout</router-link>
    </li>
  </ul>

 </div>
 </nav>
 </template>

<script>
import EventBus from './EventBus'
export default {
 data () {
return {
  auth: '',
  user: ''
  }
 },
 methods: {
 logout () {
  localStorage.removeItem('usertoken')
 }
},
mounted () {
EventBus.$on('logged-in', status => {
  this.auth = status
 })
}
}

这是路由器文件夹的index.js

import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/components/Home'
import Login from '@/components/Login'
import Register from '@/components/Register'
import Profile from '@/components/Profile'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/',
      name: 'Home',
      component: Home
    },
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/register',
      name: 'Register',
      component: Register
    },
    {
      path: '/profile',
      name: 'Profile',
      component: Profile
    }
  ]
})

1 个答案:

答案 0 :(得分:1)

您在上一个链接中缺少to。将href替换为to,错误就会消失。

     <li v-if="auth == 'loggedin'" class="nav-item">
      <router-link class="nav-link" to="">Logout</router-link>
    </li>

关于name的未定义错误也应该消失。这可能是因为它找不到由undefined

定义的键为router-link的路由

如果这不能解决问题,则可能需要发布更多代码。