Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue router with webpack basic example

I'm trying to create a really basic VueRouter example so I can understand how things are working better.

I have the following example:

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

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

import Message from './components/Message.vue';

new Vue({
    el: '#app',
    routes: new VueRouter({
        '/': {
            component: Message
        }
    })
})

Message.vue

<template>
    <div class="">Message</div>
</template>

<script>
    export default {
    }
</script>

But nothing seems to show and I'm not sure what I'm missing. I figure I need render but I'm not really sure what object i'd be rendering.

What have I left out?

like image 215
user1157885 Avatar asked Nov 25 '25 07:11

user1157885


1 Answers

There are several things wrong here.

First, routes is an array of objects.

const routes = [
  {path: "/", component: Message}
]

Second, for clarity, define your router outside of the Vue definition.

const router = new VueRouter({
  routes
})

In your Vue definition, the router must be called router.

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

Finally, in your template for #app, you'll need a <router-view></router-view>.

like image 127
Bert Avatar answered Nov 26 '25 20:11

Bert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!