Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
22
rated 0 times [  24] [ 2]  / answers: 1 / hits: 15950  / 7 Years ago, mon, january 16, 2017, 12:00:00

I have started playing with Vue but faced an issue when trying to pass data to a component using props. In the code below this.myData (in the Hello.vue) is undefined for some reason



App.vue



<script>
import Hello from './components/Hello'

export default {
name: 'app',
data() {
return {
myData: 'this is my data'
}
},
components: {
Hello
} ...
</script>


Hello.vue



<script>
export default {
name: 'hello',
props: ['myData'],
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
}
mounted: function() {
console.log(this.myData)
}
} ...
</script>

More From » vue.js

 Answers
20

You need to use the child component in the parent like, so:


// inside app.vue
<template>
<hello :myData='myData'></hello> // I believe this is your objective
</template> //:myData is binding the property from the components data field, without using it, you will get myData as `string` in the child component.

<script>
export default {
name: 'app',
data() {
return {
myData: 'this is my data'
}
},
components: {
Hello
}
</script>

OP requested a way to pass in props to a child without rendering the child in the template.


So I am posting a link to the documentation


Vue.component('hello', {
render: function (createElement) {
return createElement(
<tag-names>
)
},
props: {
myData: parentComponent.myData // you need to give the parent components' data here.
}
})

[#59346] Thursday, January 12, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
vanessag

Total Points: 170
Total Questions: 98
Total Answers: 88

Location: El Salvador
Member since Sun, Sep 12, 2021
3 Years ago
;