vue3父组件点击按钮向子组件传值样例代码
时间: 2023-06-08 08:04:13 浏览: 164
请参考以下代码:
父组件:
```
<template>
<div>
<button @click="sendDataToChild">向子组件传值</button>
<ChildComponent :data="parentData"></ChildComponent>
</div>
</template>
<script>
import ChildComponent from './ChildComponent.vue';
export default {
components: {
ChildComponent,
},
data() {
return {
parentData: '父组件传递给子组件的数据',
};
},
methods: {
sendDataToChild() {
this.parentData = '更新后的父组件数据';
},
},
};
</script>
```
子组件:
```
<template>
<div>{{ data }}</div>
</template>
<script>
export default {
props: {
data: {
type: String,
default: '',
},
},
};
</script>
```
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)