admin管理员组文章数量:1312652
I have started playing with Vue
but faced an issue when trying to pass data to a ponent using props
. In the code below this.myData
(in the Hello.vue
) is undefined
for some reason
App.vue
<script>
import Hello from './ponents/Hello'
export default {
name: 'app',
data() {
return {
myData: 'this is my data'
}
},
ponents: {
Hello
} ...
</script>
Hello.vue
<script>
export default {
name: 'hello',
props: ['myData'],
data () {
return {
msg: 'Wele to Your Vue.js App'
}
}
mounted: function() {
console.log(this.myData)
}
} ...
</script>
I have started playing with Vue
but faced an issue when trying to pass data to a ponent using props
. In the code below this.myData
(in the Hello.vue
) is undefined
for some reason
App.vue
<script>
import Hello from './ponents/Hello'
export default {
name: 'app',
data() {
return {
myData: 'this is my data'
}
},
ponents: {
Hello
} ...
</script>
Hello.vue
<script>
export default {
name: 'hello',
props: ['myData'],
data () {
return {
msg: 'Wele to Your Vue.js App'
}
}
mounted: function() {
console.log(this.myData)
}
} ...
</script>
Share
Improve this question
edited Jan 16, 2017 at 5:12
Amresh Venugopal
9,5495 gold badges41 silver badges52 bronze badges
asked Jan 16, 2017 at 3:07
Stefan StoychevStefan Stoychev
5,0223 gold badges36 silver badges54 bronze badges
1 Answer
Reset to default 5You need to use the child ponent 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 ponents data field, without using it, you will get myData as `string` in the child ponent.
<script>
export default {
name: 'app',
data() {
return {
myData: 'this is my data'
}
},
ponents: {
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.ponent('hello', {
render: function (createElement) {
return createElement(
<tag-names>
)
},
props: {
myData: parentComponent.myData // you need to give the parent ponents' data here.
}
})
本文标签: javascriptVuejspass data from parent to child with propsStack Overflow
版权声明:本文标题:javascript - Vuejs - pass data from parent to child with props - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741918626a2404876.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论