Search code examples
vue.jsvuejs2vue-routervue-loader

Bootstraping with vue 2, router and vue-loader


With vuejs 1 the code was:

const App = Vue.extend(Index);
router.start(App, '#app');

Where Index is a component.

I try to convert it to vue 2 like this:

const App = Vue.extend(Index);
const app = new App(router).$mount('#app');

But this gives an [Vue warn]: Error when rendering root instance.

What is the correct way to re-write this?

thanks


Solution

  • You have to give the router as a parameter:

    const app = new App({ router }).$mount('#app');
    

    Example:

    const Index = {
      template: `<div id="app">It's working!</div>`
    }
    const App = Vue.extend(Index)
    const router = new VueRouter()
    
    const app = new App({ router }).$mount('#app');
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
    <div id="app"></div>