uni-app 使用九宫格(uni-grid)布局组件

1、运行环境

开发工具为 HBuilder X 4.23, 操作系统为 Windows 11。Vue.js 版本为 3.

2、操作步骤

首先,登录 HBuilder X。然后用桌面浏览器,访问官网组件网址。

https://ext.dcloud.net.cn/plugin?name=uni-grid

在组件网址右上角、点击"下载插件并导入 Hbuilder X"。

此时网址有可能提示再次登录 HBuilder X 账号。完成登录后、在浏览器弹窗点击"打开 HBuilder X"。

此时 HBuilder X 会弹窗供选择将该组件导入到哪个项目。完成选择并确认后、下方的控制台会输出内容、此时便是完成组件注册。

3、代码示例

我们的代码在一个 .vue 文件中,以 menu.vue 为例。

javascript 复制代码
<template>
	<view class="warp">
		<!-- 宫格 -->
		<view class="example-body">
			<uni-grid :column="3" :highlight="true" @change="onClickChange">
				<uni-grid-item :index="i" :key="i" v-for="(menuStub,i) in gridList">
					<view class="grid-item-box" style="background-color: #fff;">
						<!-- <image :src="'/static/grid/c'+(i+1)+'.png'" class="image" mode="aspectFill" /> -->
						<!--text class="big-number">{{i+1}}</text-->
						<text class="text">{{menuStub.mName}}</text>
					</view>
				</uni-grid-item>
			</uni-grid>
		</view>	
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 固定的菜单示例
				gridList: [{mName:'点检填报'},{mName:'保养填报'},{mName:'点检记录'},{mName:'保养记录'},{mName:'e'},{mName:'f'}]
			}
		},
		methods: {
			onClickChange(e){
				console.log("点击的位置为:",e.detail.index);
				//begin 判断跳转
				
				//end 判断跳转
			}
		}
	}
</script>

<style>
	page {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		background-color: #fff;
		min-height: 100%;
		height: auto;
	}
	view {
		font-size: 14px;
		line-height: inherit;
	}
	.example-body {
		display: flex;
		flex-direction: row;
		flex-wrap: wrap;
		justify-content: center;
		padding: 0;
		font-size: 14px;
		background-color: #ffffff;
		flex-wrap: wrap; /* add 240813 */
	}
	
	.section-box{
		display: flex;
		flex-direction: row;
		align-items: center;
		padding: 20rpx;
	}
	.decoration{
		width: 4px;
		height: 12px;
		border-radius: 10px;
		background-color: #2979ff;
	}
	.section-text{
		color: #333;
		margin-left: 15rpx;
	}

	.warp {
		background-color: #fff;
	}

	.example-body {
		flex-direction: column;
		padding: 15px;
		background-color: #ffffff;
	}

	.image {
		width: 50rpx;
		height: 50rpx;
	}
	
	.big-number{
		font-size: 50rpx;
		font-weight: 700;
		font-stretch: condensed;
		font-style:oblique;
	}
	
	.text {
		text-align: center;
		font-size: 26rpx;
		margin-top: 10rpx;
	}

	.example-body {
		display: block;
	}

	.grid-item-box {
		flex: 1;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 15px 0;
	}
	
	.grid-item-box-row {
		flex: 1;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		padding: 15px 0;
	}

	.banner-image {
		width: 750rpx;
		height: 400rpx;
	}

	.swiper-box {
		height: 400rpx;
	}

	.search-icons {
		padding: 16rpx;
	}

	.search-container-bar {
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		position: fixed;
		left: 0;
		right: 0;
		z-index: 10;
		background-color: #fff;
	}

	::v-deep
	.uni-searchbar__box {
		border-width: 0;
	}

	::v-deep
	.uni-input-placeholder {
		font-size: 28rpx;
	}
</style>

4、总结

记得先在 HBuilder X 导入组件、再开始开发。(完)

相关推荐
web打印社区2 天前
HTML5 静默打印:前端页面怎么调用打印机且尽量不弹窗
前端·javascript·vue.js·pdf·html·html5
下家3 天前
为了脱离前端鄙视链,于是自己写个框架 - React 党看完沉默了
前端·前端框架
程序鉴定师4 天前
2026年深圳小程序/App开发公司技术选型指南:从架构设计到交付标准全维度评估
java·小程序·uni-app·php·objective-c
Q26433650234 天前
【有源码】基于uni-app的旅游行程规划小程序-基于微信小程序的智慧文旅综合服务平台
java·微信小程序·小程序·uni-app·毕业设计·springboot·源代码
派小心.4 天前
React StrictMode埋点双执行的验收方法
前端·前端框架
星河耀银海4 天前
数据解析:AI返回JSON数据在HTML5中的渲染方法
人工智能·json·html5
qq_316837754 天前
阿里云百炼 fun-asr-realtime uniapp vue3 调用
javascript·vue.js·uni-app
JudithHuang4 天前
React 常用 Hooks
前端·react.js·前端框架
码云之上4 天前
从搜索框到 Agent:Chatbot 联网搜索的技术演进
人工智能·架构·前端框架