【示例】:父组件将?title 和 count 传递给子组件?(普通数据 和 响应式数据的传递)
stage 1:申明 (即 父组件:定义方法 + 子组件:调用方法)
如:
vue中有4-5种,本节介绍最常见的?
普通数据:title="hello world" ,无法响应式修改数据
响应式数据 :count="count"
<body>
<div id="app">
<my-head title="hello world" :count="count"></my-head>
</div>
<script>
let app = Vue.createApp({
data(){
return {
count: 10,
title: "hello vue3"
}
},
mounted(){
setTimeout(()=>{
this.title = "hello vue3";
this.count = 20;
}, 2000)
}
})
app.component('MyHead', {
props: {
'count': {
type: Number
}
},
props: ['title', 'count'],
template: `
<header>
<div>{{ title }},{{ count }}</div>
<h2>logo</h2>
<ul>
<li>首页</li>
<li>视频</li>
<li>音乐</li>
</ul>
</header>
`
});
let vm = app.mount('#app');
</script>
</body>
<body>
<div id="app">
<div>{{message}}</div>
<my-head @custom-click="handleClick"></my-head>
</div>
<script>
let app = Vue.createApp({
data(){
return {
message: "app Data"
}
},
methods: {
handleClick(data){
// console.log(data);
this.message = data;
}
}
})
app.component('MyHead', {
emits: ['custom-click'],
template: `
<header>
<h2>logo</h2>
<ul>
<li>首页</li>
<li>视频</li>
<li>音乐</li>
</ul>
</header>
`,
mounted(){
setTimeout(()=>{
this.$emit('custom-click', 'MyHead Data')
}, 2000)
}
});
let vm = app.mount('#app');
</script>
</body>