【发布时间】:2022-01-16 20:18:19
【问题描述】:
在 Vue2 中,我试图访问子组件的数据,然后在不触发事件的情况下放入父组件的数据。在下面的例子中,我想把 count:20 保存到父组件中,如果有错误请告诉我,谢谢!
子组件
<template>
<div></div>
</template>
<script>
export default {
data() {
return {
count: 20,
};
},
};
</script>
父组件
<template>
<div>
<child ref="child1"></child>
{{count}}
</div>
</template>
<script> import child from './child.vue'
export default {
components: {
child
},
data() {
return{
count:this.$refs.child1.count
}
},
}
</script>
VScode 中的警告信息
“Vue | 类型”上不存在属性“count”元素 | Vue[] |元素[]'。 类型“Vue”上不存在属性“count”。
浏览器中的警告消息
[Vue 警告]:data() 中的错误:“TypeError: undefined is not an object (evalating 'this.$refs.child1')”
【问题讨论】:
标签: vue.js components vue-component ref