Skip to content

Instantly share code, notes, and snippets.

@mjhanninen
Created December 30, 2016 08:58
Show Gist options
  • Save mjhanninen/d9cb707c6419588f36b93a936470aeae to your computer and use it in GitHub Desktop.
Save mjhanninen/d9cb707c6419588f36b93a936470aeae to your computer and use it in GitHub Desktop.
Fixes to A. Sanjeevs' Vue.js tutorial

Fixes to A. Sanjeevs' Vue.js tutorial

If you work through Anirudh Sanjeevs' excellent tutorial on setting up a Vue.js project with Webpack and some other goodies, you'll probably hit a handful of minor problems. The tutorial was written a year ago and things have changed a lot since then which naturally explains the issues. So let's tackle them one by one.

The first problem is that Vue.js fails to mount the app component and raises the following error:

Failed to mount component: template or render function not defined. (found in root instance)

This happens because the template defined in app.vue cannot be compiled with the runtime-only build of Vue.js that the NPM package exports by default. It is missing the template compiler that, instead, can be found from the standalone build. So to use the standalone build we need to add the following alias to webpack.config.js:

resolve: {
    alias: {
        'vue$': 'vue/dist/vue.common.js'
    }
}

The differences between the runtime-only and standalone builds are discussed in more detail in the installation documentatien of Vue.js.

But Vue.js still fails to mount the app component. This time with a different error though:

Do not mount Vue to or - mount to normal elements instead.

The obvious fix is to mount to the <app></app> element instead. So we change src/main.js to:

import Vue from 'vue'
import App from './app.vue'

new Vue({
    el: 'app',
    components: { App }
})

After this fix the component mounts but there is still one issue remaining. The browser renders only the message element missing the "Increment" link and the framework reports the following error:

Component template should contain exactly one root element ...

The obvious fix this time is to wrap the template inside a <div></div>. So we change src/app.vue to:

<template>
<div>
    <div class="message">Value is: {{ count }}</div>
    <a href="#" @click.prevent="increment">Increment</a>
</div>
</template>

...

Now it works.

@andreasvirkus
Copy link

andreasvirkus commented Jan 9, 2017

Thanks! Was stuck with the same issue (first)!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment