vue-router 不渲染组件,而是更改 this.$router.push 上的 url

Jav*_*cke 6 vue.js vue-router vue-component vuejs2

我试图在按下按钮后重定向到菜单,我已经按照本教程进行操作, 但它不起作用,

当按下我的按钮时,url 会更新,但保持在相同的视图中,它还将/#/添加到我的 url 中,而不是按照我在 routes.js 中编码的内容进行操作

我在控制台上收到下一个错误

 Uncaught (in promise) 
NavigationDuplicated {
_name: "NavigationDuplicated", 
name: "NavigationDuplicated", 
message: "Navigating to current location ("/menu") is not allowed", stack:
Run Code Online (Sandbox Code Playgroud)

当按下按钮时,url 变成http://localhost:8080/#/menu而不是 http://localhost:8080/menu

如果我手动输入网址http://localhost:8080/menu就会变成这个 http://localhost:8080/menu/#/

请帮忙,我对 vuejs 还很陌生

这是我项目的结构

在此处输入图片说明

主文件

import Vue from 'vue'
import App from './App.vue'
import VueRouter  from 'vue-router'
import vuetify from './plugins/vuetify'
import routes from './routes'
import 'roboto-fontface/css/roboto/roboto-fontface.css'
import '@mdi/font/css/materialdesignicons.css'

Vue.config.productionTip = false
Vue.use(VueRouter)
const router = new VueRouter({routes});

new Vue({
  render: h => h(App),
  router,
  vuetify
}).$mount('#app')
Run Code Online (Sandbox Code Playgroud)

应用程序

<template>
  <div id="app">
    <Home/>
  </div>
</template>

<script>
import Home from './views/Home.vue'
import 'material-design-icons-iconfont/dist/material-design-icons.css';

export default {
  name: 'App',

  components: {
    Home
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>
Run Code Online (Sandbox Code Playgroud)

我的路线.js

import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from './views/Home.vue'
import About from './views/About.vue'
import TraceabilityMenu from './views/TraceabilityMenu.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', component: Home, name: 'home' },
  { path: '/menu', component: TraceabilityMenu, name: 'traceability-menu' },  
  {path: '/about', component: About, name: 'about'}
]
export default routes;

Run Code Online (Sandbox Code Playgroud)

我的 Home.vue 这是要加载的第一个视图(通过 App.vue)

<template>
  <v-app id="inspire">
    <v-app-bar app color="indigo" dark>
      <v-toolbar-title>Project Traceability</v-toolbar-title>
      <template>
        <v-spacer />
          <v-btn color="primary" @click="showPopupLogin()" :to="{ name: 'login'}" >Ingresar</v-btn>
      </template>

    </v-app-bar>
    <PopupLogin v-show="showLogin"/>
    <v-content>
      <v-container
        class="fill-height"
        fluid
      >
        <v-row
          align="center"
          justify="center"
        >
          <v-col class="text-center">
          </v-col>
        </v-row>
      </v-container>
    </v-content>
    <v-footer
      color="indigo"
      app
    >
    </v-footer>
  </v-app>
</template>

<script>
import PopupLogin from '@/components/PopupLogin.vue';
  export default {
    props: {
      source: String,
    },
    data: () => ({
      showLogin     : false
    }),
    components: {
        PopupLogin,
    },
    methods: {
      showPopupLogin() {
        this.showLogin = !this.showLogin
      }
    }
  }
</script>

Run Code Online (Sandbox Code Playgroud)

组件 PopupLogin

<template>
  <v-app id="inspire">
    <v-content>
      <v-container class="fill-height" fluid>
        <v-row align="center" justify="center">
          <v-col cols="12" sm="8" md="4">
            <v-card class="elevation-12">
              <v-toolbar color="primary" dark flat >
                <v-toolbar-title>Iniciar sesión</v-toolbar-title>
                <v-spacer />
                <v-tooltip bottom>
                </v-tooltip>
              </v-toolbar>
              <v-card-text>
                <!-- Formulario de login-->
                <v-form v-model="validForm" ref="formLogin">
                  <v-text-field 
                    required 
                    label="Usuario" 
                    :rules="nameRules" 
                    name="login" 
                    type="text" 
                    v-model="existingUser.username"/>
                  <v-text-field 
                    required 
                    id="password" 
                    prepend-icon="lock" 
                    label="Contraseña" 
                    name="password" 
                    type="password"
                    v-model="existingUser.password"/>
                </v-form>
              </v-card-text>

              <v-card-actions>
                <v-spacer/>
                <v-btn color="primary" @click="loginUser()">Ingresar</v-btn>
              </v-card-actions>
            </v-card>
          </v-col> 
        </v-row>
      </v-container>
    </v-content>
  </v-app>
</template>


<script>
  export default {
    name: 'PopupLogin',
    props: {
      source: String
    },
    data: () => ({
      validForm     : false,
      //objetos
      existingUser  : {}
    }),
    methods: {
      //Funcion que llamara al servicio de login en backend
      loginUser() {
        this.$router.push({path: '/menu'});
      }
    }
  }
</script>
Run Code Online (Sandbox Code Playgroud)

TraceabilityMenu.vue 按下按钮登录后我试图呈现的视图

<template>
  <v-app id="inspire">
   <div>RENDER ME!</div>
  </v-app>
</template>

<script>
  export default {
    props: {
      source: String,
    },
    data: () => ({
      drawer: null,
    }),
  }
</script>
Run Code Online (Sandbox Code Playgroud)

Fáb*_*reu 3

在你的 main.js 文件中尝试更改

const router = new VueRouter({routes});
Run Code Online (Sandbox Code Playgroud)

const router = new VueRouter({routes, mode: 'history'});
Run Code Online (Sandbox Code Playgroud)

编辑:还要检查您的根组件 App.vue 中是否包含 router-view 标签。

  • 我明白了,您是否在根组件 App.vue 中包含了 router-view 标签? (2认同)