Ahmad Badpey
Ahmad Badpey

Reputation: 6612

router-link does not create links properly in Vuejs

I want to create simple links to components via Vue-router plugin.

For that I created a routes.js file like this :

import VueRoute from 'vue-router';

let routes = [
    {
        path: '/',
        component: './components/Home'
    },
    {
        path: '/About',
        component: './components/About'
    }
];

export default new VueRoute({
    routes
});

And there is app.js file like this :

import Vue from 'vue';
import VueRouter from 'vue-router';
import axios from 'axios';
import router from 'routes';

window.Vue   = Vue;
window.axios = axios;
Vue.use(VueRouter);

new Vue({
    el: '#app',
    router
});

And this is my master page content :

<!doctype html>
<html lang="{{ config('app.locale') }}">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="/css/app.css">
    <title>My First Laravel Vue App </title>
</head>
<body>
<div id="app">

    <router-link to="/">Home</router-link>
    <router-link to="/About">About</router-link>

</div>

<script src="/js/app.js"></script>
</body>
</html>

But After compiling all js files when I open page,router-link does not work and just creates a simple empty html comment instead.

I do not know what is problem and how Can I solve that.

Update:

I changed import router from 'routes'; to import router from './routes' and all things worded fine!

Upvotes: 0

Views: 1717

Answers (1)

Vamsi Krishna
Vamsi Krishna

Reputation: 31352

you have everything setup but fforgot the router-view

Add it like this:

<body>
<div id="app">

    <router-link to="/">Home</router-link>
    <router-link to="/About">About</router-link>

    <router-view></router-view>
</div>

<script src="/js/app.js"></script>
</body> 

The matched routes are rendered in the router-view

EDIT

The component property of the route object should be the component itself but you are passing it a string

So do it like this:

    import VueRoute from 'vue-router';

import Home from './components/Home'
import About from './components/About')

let routes = [
    {
        path: '/',
        component: Home
    },
    {
        path: '/About',
        component: About
    }
];

export default new VueRoute({
    routes
}); 

Upvotes: 4

Related Questions