<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<cpn></cpn>
<cpn></cpn>
<my-cpn></my-cpn>
<me-cpn></me-cpn>
<cpn ref='aaa'></cpn>
<button @click='btnClick'>按钮</button>
</div>
<template id="cpn">
<div>我是子组件</div>
</template>
<script crossorigin="anonymous" integrity="sha512-YXLGLsQBiwHPHLCAA9npZWhADUsHECjkZ71D1uzT2Hpop82/eLnmFb6b0jo8pK4T0Au0g2FETrRJNblF/46ZzQ==" src="https://lib.baomitu.com/vue/2.6.12/vue.js"></script>
<script>
const app=new Vue({
el:'#app',
data:{
message:'你好啊'
},
methods:{
btnClick(){
/* console.log(this.$children)
for(let c of this.$children){
console.log(c.name)
c.showMessage()
} */
///console.log(this.$children[2].name)
//2.$refs =>对象类型,默认是一个空的对象
console.log(this.$refs)
}
},
components:{
cpn:{
template:'#cpn',
data(){
return {
name:'我是子组件的name'
}
},
methods:{
showMessage(){
console.log('showMessage')
}
}
}
}
})
</script>
</body>
</html>
父访问子组件 -children-refs的基本使用
猜你喜欢
转载自blog.csdn.net/qq_42526440/article/details/114840635
今日推荐
周排行