admin管理员组

文章数量:1435859

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 ponent

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

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

And here is the main code

Vueponent('example', require('./ponents/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 ponent )

Any ideas whats wrong? Thanks

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 ponent

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

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

And here is the main code

Vue.ponent('example', require('./ponents/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 ponent )

Any ideas whats wrong? Thanks

Share Improve this question edited Oct 8, 2016 at 15:06 m_pro_m asked Oct 8, 2016 at 12:03 m_pro_mm_pro_m 3404 silver badges12 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

In your script tags instead of export default use:

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

This should work for you

Call the ponent inside your template

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

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

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

The problem is that you are trying to load the ponent '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 ponent the following way (not sure if extension .vue is needed):

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

If you are using Babel you can also load the ponents 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

本文标签: javascriptVueJS data() not workingStack Overflow