uniapp自定义组件和父子组件传值
2024-04-22 本文已影响0人
林希品
![](https://img.haomeiwen.com/i2782369/1a12962310548559.png)
bilibili视频教程
https://www.bilibili.com/video/BV1oy4y1j75s?p=9&spm_id_from=pageDriver&vd_source=98cd809e741003b47a716078038cd594
主要代码
1.父组件中的代码
![](https://img.haomeiwen.com/i2782369/a772ca5ba045dc00.png)
2.子组件中的代码
![](https://img.haomeiwen.com/i2782369/f623ff8903e0eddc.png)
index To me页面中的页面传值
![](https://img.haomeiwen.com/i2782369/24b5cfcc92f5fda9.png)
![](https://img.haomeiwen.com/i2782369/cd0e5c0f3d072984.png)
对象传值
1.父组件 Parent.vue:
<template>
<view>
<child :data="dataFromParent"></child>
</view>
</template>
<script>
import child from './components/Child.vue';
export default {
components: {
child
},
data() {
return {
dataFromParent: {
name: 'John',
age: 30
}
};
}
}
</script>
2.子组件 Child.vue:
<template>
<view>
<text>Name: {{ data.name }}</text>
<text>Age: {{ data.age }}</text>
</view>
</template>
<script>
export default {
props: ['data'],
data() {
return {
// 在子组件中创建一个新的变量,并将父组件传递的对象复制给这个变量
internalData: {}
};
},
created() {
// 在子组件的created生命周期中,将父组件传递的对象复制给内部变量
this.internalData = { ...this.data };
}
}
</script>