1

I am creating website using node.js, express, pug, vue.js. I have one problem regarding Vue.js

app.js

window.Vue = require('vue');
Vue.component('work-list', require('../views/components/WorkList.vue'));

const app = new Vue({
  el: '#app',
  data: {
    message: 'Welcome'
  }
});

WorkList.vue

<script>
  export default {
    name: 'WorkList',
    data: function () {
      return {
        message: 'WorkList Page'
      }
    }
  }
</script>

index.pug

(HTML Code..)
body
    #app.container
        h1 {{ message}}
        .row
            block content
        a(href="/worklist") Go to worklist
  script(src="bundle.js")

worklist.pug

extends index.pug

block content
work-list {{ message }}

When I try to access worklist page, browser is returning following error message.

enter image description here

How can I make WorkList in Root element?

1

5 Answers 5

8

This might also occur even when a <template></template> is defined in your Vue single file component. In addition to require('/path/to/your/Component.vue') as the second parameter to Vue.component() when registering the component globally in your app.js, you also have to specify .default:

Vue.component('work-list', require('../views/components/WorkList.vue').default);
                                                                      ^^^^^^^^
Sign up to request clarification or add additional context in comments.

2 Comments

This must have changed from an older Vue version. I dusted off a project after a few months and this fixed this issue.
I would give you more than 1 upvote if I could, thanks.
4

Your component file 'WorkList.vue' needs a template defined. The most straight forward way to construct a '.vue' component is with three sections.

<template>
   //template code goes here.
</template>

<script>
   // vue javascript here
</script>

<style>
   // applicable styles here
</style>

Your component is expecting the <template> markup.

There are other ways to define the template as well. https://v2.vuejs.org/v2/guide/render-function.html

When defining your component

Vue.component('my-component', {
    template: `<div> markup here</div>`,
    data(){
      ...
    }
});

X-Templates (similar to above)

Vue.component('my-component', {
    template: `#someElementId`,
    ....

 //then in a separate component.js file

 <script type="text/x-template" id="someElementId">
   <div> markup here </div>
</script>

Using the render function

export default {
    data() {
     ...
    }
    render(createElement) {
        return createElement(
            'div', {....}
        ....
 

3 Comments

I tried Vue.component('work-list', { template: (...) }); it works. But <template>...</template><script>...</script> in WorkList.vue doesn't work. Returned same error. WorkList element out of Root element.
Are you building your .vue component using webpack? I have never used Pug as a pre-processor so it may be a problem there?
I changed app.js file. Using import WorkList from '...'; and component to register app. It works. Thanks for reply.
3

Following on from Erich's answer, if you are using ES6 imports it would be:

import WorkList from '../views/components/WorkList'; // .vue is extension not required

Vue.component('work-list', WorkList);

Comments

2

In my case, I forgot to put my dynamic import inside an arrow function.

Wrong:

{
    path: "/example/",
    component: import("@/pages/Example"),
}

Correct:

{
    path: "/example/",
    component: () => import("@/pages/Example"),
}

Comments

0

I believe the problem is the alias for vue in your webpack config.

Check out the different types of Vue builds.

Try using vue.min.js or vue.common.js

//webpack.config.js
...
alias: {
  'vue$': 'vue/dist/vue.common.js'
},
...

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.