当前位置: 首页 > news >正文

Vue3:component(组件:uniapp版本)

目录

  • 一、基本概述
  • 二、基本使用
  • 三、插槽


一、基本概述

在项目的开发过程中,页面上井场会出现一些通用的内容,例如头部的导航栏,如果我们每一个页面都去写一遍,那实在是太繁琐了,所以,我们使用组件来解决这一难题。

二、基本使用

下面的案例中,我们定义一个用户信息展示的组件,通过传递一个对象来展示用户的头像以及姓名。
在这里插入图片描述
首先我们需要定义一个用户信息展示的组件user-info,uniapp中定义组件需要定义在根目录下的components中

<template><view class="box"><image :src="userImage" mode="aspectFill" class="user-image"></image><view class="user-name">{{userName}}</view></view>
</template><script setup>
import { computed } from 'vue';const props = defineProps({userName: {type: String, default: "匿名"},userImage: {type: String, default: "../../../static/logo.png"},obj: {type: Object, default() {return {userName: "匿名", userImage: "../../../static/logo.png"}}}
})
const userNameT = computed(() => props.userName + "@")
</script><style lang="scss" scoped>
.box {margin: 5rpx;width: 100%;height: 250rpx;background-color: #ccc;display: flex;flex-direction: column;align-items: center;justify-content: center;.user-image {width: 200rpx;height: 200rpx;}.user-name {margin-top: 5rpx;width: 100%;text-align: center;font-size: 14rpx;}
}
</style>

在主页面调用组件,uniapp会自动检索components下面的组件,所以在页面中不需要引入

<template><view class="box"><page-head title="component 组件"></page-head><view class="uni-padding-wrap uni-common-mt uni-flex uni-row uni-container"><user-info v-for="item in userList" :user-name="item.userName" :user-image="item.userImage"></user-info></view></view>
</template><script setup>
import { ref } from 'vue';const userList = ref([{userName: "张三", userImage: "../../../static/logo.png"},{userName: "李四", userImage: "../../../static/java.jpeg"},{userName: "王五", userImage: "../../../static/mybatis.png"},{userName: "赵六", userImage: "../../../static/mysql.jpeg"},
])
</script><style lang="scss" scoped>.uni-container {-webkit-flex-wrap: wrap;flex-wrap: wrap;}
</style>

三、插槽

我们在定义组件的时候,有的时候个别部分是不通用的,如果都单独创建一个特定的组件,那将会失去组件的真实意义,因此,vue为我们提供了slot插槽标签帮我们解决了这一问题,将单独部分进行定制。
在这里插入图片描述
首先我们需要定义一个标题组件,在特定的区域,我们使用slot插槽进行留白。

<template><view class="box"><view class="box-title-t">{{title}}</view><view><slot name="icon"></slot></view></view>
</template><script setup>
const props = defineProps({title: {type: String, default: "这是啥?"}
})</script><style lang="scss" scoped>
.box {padding: 0 10rpx;width: 100%;height: 100rpx;display: flex;flex-direction: row;align-items: center;justify-content: space-between;.box-title-t {color: #000;font-weight: 700;}
}
</style>

在主页面中我们引入组件,并对组件进行定制,这里需要注意的是,插槽里面的样式要写在主页面中,如果写在组件里面,那会成为公共样式。

<template><view class="box"><page-head title="component 组件"></page-head><view class="uni-padding-wrap uni-common-mt uni-flex uni-row uni-container"><title-info title="精彩呈现" class="title-class"><template #icon><image src="/static/logo.png" mode="aspectFill"></image></template></title-info><title-info title="其他内容" class="title-class"><template #icon><text>More+</text></template></title-info></view></view>
</template><script setup></script><style lang="scss" scoped>.title-class {image {width: 50rpx;height: 50rpx;}text{color: #ccc;}}
</style>

相关文章:

  • gem5教程第五章 了解gem5默认配置脚本
  • Vue3父子组件数据同步方法
  • AtCoder Beginner Contest 402题解
  • 云原生--CNCF-3-核心工具介绍(容器和编排、服务网格和通信、监控和日志、运行时和资源管理,安全和存储、CI/CD等)
  • 华为开发岗暑期实习笔试(2025年4月16日)
  • 配置kafka与spark连接
  • Unity导入GLB模型 需要用到这个插件
  • 基于CBOW模型的神经网络词向量转换原理与实践
  • 音视频小白系统入门课-4
  • leetcode-位运算
  • 搭建TypeScript单元测试环境
  • JWT的token泄露要如何应对
  • win10 快速搭建 lnmp+swoole 环境 ,部署laravel6 与 swoole框架laravel-s项目3
  • QT 打包安装程序【windeployqt.exe】报错c000007d原因:Conda巨坑
  • CIFAR-10图像分类学习笔记(一)
  • 同样的接口用postman/apifox能跑通,用jmeter跑就报错500
  • HarmonyOS Grid 网格列表可长按 item 拖动移动位置
  • Shopee五道质检系统重构东南亚跨境格局,2025年电商游戏规则悄然改写
  • QT容器类控件及其属性
  • 文件属性隐写
  • 目前中美未进行任何经贸谈判,外交部、商务部再次表明中方立场
  • 商务部新闻发言人:截至目前,中美之间未进行任何经贸谈判
  • A股三大股指涨跌互现,工农中三大行股价创新高
  • 中华人民共和国和肯尼亚共和国关于打造新时代全天候中非命运共同体典范的联合声明
  • 嫦娥八号任务合作项目,这十个入选
  • 预订假期酒店却被告知无法入住,去哪儿:对违规酒店予以处罚