Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 653
m_pro_m Avatar asked Oct 18 '22 00:10

m_pro_m


2 Answers

In your script tags instead of export default use:

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

This should work for you

like image 187
Skysplit Avatar answered Oct 20 '22 20:10

Skysplit


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>
like image 35
François Romain Avatar answered Oct 20 '22 21:10

François Romain