5

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

3 Answers 3

2

In your script tags instead of export default use:

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

This should work for you

Sign up to request clarification or add additional context in comments.

Comments

1

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>

6 Comments

Yeah, it works this way. But I would really like to use 'single file component'
this is exactly the same, no? import the single file component and replace the components options with your imported component name
Well, no. This is what I am trying to achieve, and this is what you showed me. It's not much of a difference but you know, details matter :D
are you using webpack or browserify?
Yes, Laravel has got all of this prepared
|
0

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

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.