m_pro_m
m_pro_m

Reputation: 340

VueJS data() not working

I am trying to make a VueJS app but I am failing even with the simplest examples. I am using Laravel 5.3 with pre-built support for VueJS (version 1, I tried version 2 as well).

Here is my Example.vue component

<template>
    <div class="profile">
        {{ name }}
    </div>
</template>

<script>
    export default {
        data () {
            return {
                name: 'John Doe'
            }
        }
    }
</script>

And here is the main code

Vue.component('example', require('./components/Example.vue'));

const app = new Vue({
    el: '#app'
});

This is the error that shows up everytime in console:

[Vue warn]: Property or method "name" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option. (found in component )

Any ideas whats wrong? Thanks

Upvotes: 5

Views: 6278

Answers (3)

ragnar
ragnar

Reputation: 1302

The problem is that you are trying to load the component 'example' from that file but you didn't give a name to it. You should use:

<script>
   export default {
     name: 'example',
     data() {
       return {
         name: 'John Doe'
       }
     }
   }
</script>

Or load the component the following way (not sure if extension .vue is needed):

require('./exmaple').default();

If you are using Babel you can also load the components without giving them a name using this syntax:

import Example from ./example

Also checkout this post to get some more info in case you use Babel

Upvotes: 0

Skysplit
Skysplit

Reputation: 1943

In your script tags instead of export default use:

module.exports = {
  data() {
    return { counter: 1 }
  }
}

This should work for you

Upvotes: 2

Fran&#231;ois Romain
Fran&#231;ois Romain

Reputation: 14393

Call the component inside your template

Vue.component('example', {
  template: `<div class="profile">{{ name }}</div>`,
  data () {
return {
  name: 'John Doe'
}
  }
})

const app = new Vue({
  el: '#app'
})
<script src="https://unpkg.com/vue/dist/vue.js"></script>

<div id="app"><example></example></div>

Upvotes: 1

Related Questions