持有注册过 ref 属性的所有 DOM 元素和组件实例
//this.$f属性名.子组件数据
//this.$f属性名.子组件方法
console.log(this.$refs.twoChildrenRef.name)
console.log(this.$refs.twoChildrenRef.showMessage())
父组件:
<div id="app">
<cpn></cpn>
<cpn ref="twoChildrenRef"></cpn>
<cpn></cpn>
<button @click="btnClick">获取子组件的对象</button>
</div>
<script>
const app=new Vue({
el:"#app",
components:{cpn},
methods:{
btnClick(){
//方式二、this.$f属性名.
console.log(this.$refs.twoChildrenRef.name)----调用子组件数据
console.log(this.$refs.twoChildrenRef.showMessage())----调用子组件方法
}
}
})
</script>
子组件:
<template id="cpn">
<div>我是子组件</div>
</template>
<script>
const cpn={
template:`#cpn`,
data(){
return {
name:"我是子组件的数据"
}
},
methods:{
showMessage(){
console.log("子方法showMessage")
}
}
}
本文发布于:2024-01-31 14:00:54,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170668085829037.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |