子访问父组件parent
<!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>
</div>
<template id="cpn">
<div>
<h2>cpn我</h2>
<ccpn></ccpn>
</div>
</template>
<template id="ccpn">
<div>
<h2>子组件我</h2>
<button @click="btnClick">anniu</button>
</div>
</template>
<script src="./vue.js"></script>
<script>
const app = new Vue({
el : '#app',
data : {
message : 'nihaoya'
},
components: {
cpn : {
template : '#cpn',
data(){
return {
name : 'cpnwo'
}
},
components: {
ccpn : {
template : '#ccpn',
methods: {
btnClick(){
// 访问父组件
console.log(this.$parent);
console.log(this.$parent.name)
}
}
}
}
}
}
})
</script>
</body>
</html>