uniapp笔记-自定义分类组件

思路

uniapp就是基于vue的开发,构造自定义组件就和vue的自定义一样。如下例子。

例子

在pages/index/components下建立category.vue组件,如下图所示:

javascript 复制代码
<template>
	<view class="category-top">
		分类组件
	</view>
</template>

<script>
</script>

<style>
	.category-top{
		margin-top: 100rpx;
	}
</style>

在index.vue中进行注册,完整代码如下:

javascript 复制代码
<template>
	<view class="index-box">
		<bannerVue></bannerVue>
		<categoryBox></categoryBox>
	</view>
</template>

<script>
	import bannerVue from '../../components/common/banner.vue'
	import categoryBox from './components/category.vue'
	export default {
		components: {bannerVue, categoryBox},
		data() {
			return {
				title: 'Hello'
			}
		},
		onLoad() {

		},
		methods: {

		}
	}
</script>

运行效果如下:

下面完善下category.vue相关的代码。

javascript 复制代码
<template>
	<view class="category-top">
		<view class="category-box">
			<view v-for="(item, index) in categoryList" :key="index">
				 {{item.name}}
			</view>
		</view>
	</view>
</template>

<script>
	export default{
		props:{
			categoryList: {
				type: Array,
				default: () => [
					{
						id: 1,
						name: 'java'
					},
					{
						id: 2,
						name: 'C++'
					},
					{
						id: 3,
						name: 'PHP'
					},
					{
						id: 4,
						name: 'Python'
					},
					{
						id: 5,
						name: 'JavaScript'
					}
				]
			}
		}
	}
</script>

<style>
	.category-top{
		margin-top: 100rpx;
	}
</style>

运行截图如下:

下面美化,设置一些css。

完整代码如下:

javascript 复制代码
<template>
	<view class="category-top">
		<view class="category-box">
			<view v-for="(item, index) in categoryList" :key="index">
				 {{item.name}}
			</view>
		</view>
	</view>
</template>

<script>
	export default{
		props:{
			categoryList: {
				type: Array,
				default: () => [
					{
						id: 1,
						name: 'java'
					},
					{
						id: 2,
						name: 'C++'
					},
					{
						id: 3,
						name: 'PHP'
					},
					{
						id: 4,
						name: 'Python'
					},
					{
						id: 5,
						name: 'JavaScript'
					},
					{
						id: 6,
						name: 'HTML'
					},
					{
						id: 7,
						name: 'Web'
					},
					{
						id: 8,
						name: 'ALL'
					}
				]
			}
		}
	}
</script>

<style lang="scss">
	.category-top{
		margin-top: 50rpx;
	}
	.category-box{
		display: flex;
		justify-content: space-around;
		flex-wrap: wrap;
		padding: 20rpx 30rpx 0 30rpx;
		>view{
			width: 160rpx;
			height: 70rpx;
			background: #C0C0C0;
			text-align: center;
			line-height: 70rpx;
			font-size: 26rpx;
			border-radius: 20rpx;
			overflow: hidden;
			margin-top: 15rpx;
		}
	}
</style>

运行截图如下:

再次调整,只需要一行分类,categoryList.slice(0, 4)

完整代码如下:

javascript 复制代码
<template>
	<view class="category-top">
		<view class="category-box">
			<view v-for="(item, index) in categoryList.slice(0, 4)" :key="index">
				 {{item.name}}
			</view>
		</view>
	</view>
</template>

<script>
	export default{
		props:{
			categoryList: {
				type: Array,
				default: () => [
					{
						id: 1,
						name: 'java'
					},
					{
						id: 2,
						name: 'C++'
					},
					{
						id: 3,
						name: 'PHP'
					},
					{
						id: 4,
						name: 'Python'
					},
					{
						id: 5,
						name: 'JavaScript'
					},
					{
						id: 6,
						name: 'HTML'
					},
					{
						id: 7,
						name: 'Web'
					},
					{
						id: 8,
						name: 'ALL'
					}
				]
			}
		}
	}
</script>

<style lang="scss">
	.category-top{
		margin-top: 50rpx;
	}
	.category-box{
		display: flex;
		justify-content: space-around;
		flex-wrap: wrap;
		padding: 20rpx 30rpx 0 30rpx;
		>view{
			width: 160rpx;
			height: 70rpx;
			background: #C0C0C0;
			text-align: center;
			line-height: 70rpx;
			font-size: 26rpx;
			border-radius: 20rpx;
			overflow: hidden;
			margin-top: 15rpx;
		}
	}
</style>
相关推荐
小小小小宇11 分钟前
前端实现合并两个已排序链表
前端
yngsqq29 分钟前
netdxf—— CAD c#二次开发之(netDxf 处理 DXF 文件)
java·前端·c#
mrsk31 分钟前
🧙‍♂️ CSS中的结界术:BFC如何拯救你的布局混乱?
前端·css·面试
jonssonyan33 分钟前
我自建服务器部署了 Next.js 全栈项目
前端
A了LONE37 分钟前
h5的底部导航栏模板
java·前端·javascript
专注VB编程开发20年38 分钟前
各版本操作系统对.NET支持情况(250707更新)
开发语言·前端·ide·vscode·.net
Zsnoin能1 小时前
AI + TailwindCSS快速搭建一个属于自己的TailwindCSS学习网站
前端·css
五号厂房1 小时前
聊一聊Javascript 中 hasOwnProperty和in操作之间的区别
前端
摆烂为不摆烂1 小时前
😁深入JS(六): 一文让你完全理解浏览器进程与线程
前端·javascript
qiyue771 小时前
Cursor 深度使用指南(二) - 新能力使用教程
前端·ai编程·cursor