uniapp vue3 局部注册 components 父传子

1.子组件

defineProps   

<template>
	<view class="userinfo">
		<image :src="avatar" mode="aspectFill" class="avatar"></image>
		<view class="username">{{username}}</view>
	</view>
</template>
defineProps(['username', 'avatar'])
2.父组件

传参

<UserInfo username='niao' avatar="../../static/pic1.jpg"></UserInfo>
<UserInfo username='zhangsan' avatar="../../static/pic2.jpg"></UserInfo>
<UserInfo username='lisi' avatar="../../static/pic3.jpg"></UserInfo>
注意:

1.如果要在后端显示从父组件拿来的数据  用props接收

const name = computed(() => props.username + "~~")

2.props 校验

const props = defineProps({
		username: {
			type: String,
			default: "匿名"
		},
		avatar: {
			type: String,
			default: '../../static/logo.png'
		}

	})

3.父传子之间

父组件

<template>
	<view class="content">
		 <UserInfo v-for="(item,index) in userinfo" :obj="item"></UserInfo>
	</view>
</template>

<script setup>
	import {
		ref
	} from "vue";
	const userinfo = ref([{
			name: "张三",
			avatar: "../../static/pic1.jpg"
		},
		{
			name: "李四",
			avatar: "../../static/pic2.jpg"
		},
		{
			name: "王五",
			avatar: "../../static/pic3.jpg"
		},
	])
</script>

<style>

</style>

子组件

<template>
	<view class="userinfo">
		<image :src="obj.avatar" mode=""></image>
		<view class="username">{{obj.name}}</view>

	</view>
</template>

<script setup>
	import {
		computed
	} from "vue"

	const props = defineProps({
		obj: {
			type: Object,
			default () {
				return {
					name: '匿名',
					avatar: '../../static/logo.png'
				}
			}
		}
	})
</script>

<style lang="scss" scoped>
	.userinfo {
		width: 100%;
		height: 200px;
		background: #ccc;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;

		image {
			width: 100px;
			height: 100px;
			border-radius: 50%;
		}

		.username {
			padding: 10px 0;
			font-size: 20px;
		}
	}
</style>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值