0

I need import component with data into vue.js app.

I can component registration and I used this code.

Correct Code:

  export default {
    name: 'hello',
    data () {
      return {
        msg: 'Welcome to Your Vue.js App'
      }
    },
    components: {
      'test': {
        template: '<div>msg: {{message}}</div>',
        props: {
          message: {
            type: String,
            required: true
          }
        }
      }
    }
  }

But how can I Local Registration with data?

NOT correct:

import Test from './components/Test.vue';

export default {
    name: 'hello',
    data () {
      return {
        msg: 'Welcome to Your Vue.js App'
      }
    },
    components: {
      'test': {
        template: Test,
        props: {
          message: {
            type: String,
            required: true
          }
        }
      }
    }
  }
2
  • You want a nested component to have its own data property? Just put data along with props, template etc. Commented Apr 30, 2017 at 13:38
  • Basic question first, are you using Webpack to preprocess your app? Because I can't see anything immediately incorrect about your component. Commented May 1, 2017 at 1:45

2 Answers 2

1

This should work

import Test from './components/Test.vue';

export default {
    name: 'hello',
    data () {
      return {
        msg: 'Welcome to Your Vue.js App'
      }
    },
    components: {
      Test
    }
  }

And if you want to pass props

<template>
<Test :message=msg></Test>
</template>
Sign up to request clarification or add additional context in comments.

Comments

0

I use props in component code (components/xxx.vue) and resolved it.

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.