Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VueJS Router doesn't load the component

I am using VueJS Router, but the router is not loading the components.

I have About.vue and Contact.vue just with tag to test - following is how it looks like :

<template>
  <div>
    <h1>Contact page. Welcome baby!</h1>
  </div>
</template>

This is App.vue with three router-links and router-view.

<template>
   <div>
     <h1>Routing</h1>
     <router-link to="/">Home</router-link>
     <router-link to="/about">About</router-link>
     <router-link to="/contact">Contact</router-link>
     <router-view></router-view>
  </div>
</template>

This is main.js (the paths of importing files are correct)

import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router'
import {routers} from './router'

Vue.use(VueRouter);

let router = new VueRouter({mode: 'history', routers});

new Vue({
    el:'#app',
    router,
    components: {
      'app-home' : App
    }
});

This is the JS file for the router. router.js (paths are correct)

import About from './About.vue'
import Contact from './Contact.vue'
import Home from './App.vue'

export const routers=[
    {
      path: '/' , component: Home
    },
    {
      path:'/about',component:About
    },
    {
      path:'/contact',component:Contact
    }
]

And, this is index.html

<!DOCTYPE html>
<html lang="en">
 <head>
    <meta charset="utf-8">
    <title>router</title>
 </head>
 <body>
    <div id="app">
       <app-home></app-home>
    </div>
  <script src="/dist/build.js"></script>
 </body>
</html>

When I load the page, the main page looks like the following : Loaded Main page

and when I click the each nav, nothing is changed from the main page except URL. URL becomes

http://localhost:8080/contact

http://localhost:8080/about

but not loading the component I imported.

If you need more information to give advice, feel free to ask more. And if you have any clue of this issue, I appreciate if you share here.

Thank you.

like image 411
doobean Avatar asked Dec 10 '22 10:12

doobean


1 Answers

The object key VueRouter is expecting is called routes, you are passing it routers.

Try this...

let router = new VueRouter({mode: 'history', routes: routers});

Alternatively, rename your "routers" variables to "routes". Eg

export const routes=[

and

import {routes} from './router'
// snip
let router = new VueRouter({mode: 'history', routes });
like image 199
Phil Avatar answered Dec 22 '22 16:12

Phil