1

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

2 Answers 2

4

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>

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot for the working code snippet! Now, in my app, it says Unknown custom element: <router-view>. Trying to figure out why…
If you're using npm and webpack/browserify, you have to call Vue.use(VueRouter) to make sure that vue-router register the component <router-view> @francoisromain
2

Try this (App == Index)

const App = Vue.extend(require('./App.vue'))

const router = new VueRouter({
  routes,
  mode: 'abstract'
})

new App({
  router,
}).$mount('#app')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.