<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<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">
<ncpn></ncpn>
</template>
<template id="ncpn">
<div>
<h1>我是子2组件</h1>
<button @click='btnClick'>按钮</button>
</div>
</template>
<script src="../vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: "sb"
},
components: {
cpn: {
template: '#cpn',
methods: {
getmessage(){
return 'hhh';
}
},
components: {
ncpn: {
template: "#ncpn",
methods: {
btnClick(){
console.log(this.$root);
console.log(this.$root.message);
console.log(this.$parent);
console.log(this.$parent.getmessage());
}
}
}
}
}
}
})
</script>
</body>
</html>
子组件访问父组件数据
最新推荐文章于 2024-09-13 15:40:26 发布