uniapp - 新建页面与tabBar配置

通过开源项目学习前端代码第一天,目标是创建一个文章模块的页面和底部tab按钮。

一、新建页面

在uni-app项目中,一个页面就是一个符合Vue SFC规范的vue文件。

uni-app中的页面,默认保存在工程根目录下的pages目录下。每次新建页面,都需要在pages.json中的pages列表中配置。

pages.json是页面路由文件,用来对uni-app进行全局配置,决定页面文件的路径、窗口样式、原生的导航栏、底部的原生tabbar等。

1.新建vue文件

我们在pages文件夹下,创建一个article文件夹,再在article文件夹下创建一个名为article.vue的文件。
新建vue文件

使用HBuilderX内置的默认模板生成vue文件后的内容如下

// article.vue文件
<template>
	<!-- 模板组件区:
		 页面元素布局,html标签 -->
</template>

<script>
	<!-- 脚本区
		js代码,业务逻辑处理,接口请求 -->
	export default {
		data() {
			return {
			}
		}
	}
</script>

<style>
	<!-- 样式区
		 页面元素样式:背景色、字体色、圆角渐变等.. --> 
</style>

2.pages.json配置

新建完vue文件后,需要到pages.json中添加对应的对象

{
	"pages": [
		{
			"path": "pages/index/index", //名字叫不叫index无所谓,位置在第一个,就是首页
			"style": {
				"navigationBarTitleText": "首页" //页面标题
			}
		},
		// ...
		// article.vue对应配置 >>>>>>>>
		{
			"path": "pages/article/article",
			"style": {
				"navigationBarTitleText": "文章模块"
			}
		},
	],
	// ...
}

二、tabBar配置

vue页面创建完毕后,我们需要一个页面入口。我们在底部配置一个文章tab,在pages.json中的tabBar对象中配置,tabBar可设置2~5个tab。

1.pages.json配置

{
	"pages": [
		{
			"path": "pages/index/index", //名字叫不叫index无所谓,位置在第一个,就是首页
			"style": {
				"navigationBarTitleText": "首页" //页面标题
			}
		},
		// ...
		// article.vue对应配置 >>>>>>>>
		{
			"path": "pages/article/article",
			"style": {
				"navigationBarTitleText": "文章模块"
			}
		},
	],
	// ...
	,
	"tabBar": {
     	// tabBar 设置底部 tab 的表现
	    "color": "#909399",
	    "selectedColor": "#2979ff",
		"borderStyle": "white",
	    "backgroundColor": "#ffffff",
	    "list": [{
	        "pagePath": "pages/index/index",
	        "iconPath": "static/index.png",
	        "selectedIconPath": "static/index-selected.png",
	        "text": "首页"
	    }, {
	    	// 为新的article页面配置到底部tab栏
	        "pagePath": "pages/article/article",
	        "iconPath": "static/location.png",
	        "selectedIconPath": "static/location-selected.png",
	        "text": "文章"
	    }, {
	        "pagePath": "pages/center/center",
	        "iconPath": "static/center.png",
	        "selectedIconPath": "static/center-selected.png",
	        "text": "我的"
	    }]
	}
}

此时运行的页面显示如下,我们得到了一个文章底部tab栏和一个空白的页面
在这里插入图片描述
我们需要在vue文件中继续补充信息,使空白页面丰富起来,达到我们的目标样式。简单添加一个头部导航栏标题和一行文字。

// article.vue
<template>
	<!-- uview 内置样式 -->
	<view class="u-p-l-10 u-p-r-10">
		<!-- u-navbar导航栏组件,主要用于头部导航 -->
		<u-navbar :is-back="false" title="文章模块" :border-bottom="false">
			
		</u-navbar>
		<view>
			文章列表待更新...
		</view>
	</view>
</template>
...

最终效果:
在这里插入图片描述

三、尾声

本文已跳过nodejs下载安装、npm安装等内容。
建议使用HBuilderX编译器。

参考文章

uni-app官网文档

### UniApp 全局 TabBar 集成教程 在 UniApp 中实现全局 TabBar 功能,需要通过配置 `pages.json` 文件以及自定义 TabBar 来完成。以下是详细的实现方法: #### 1. 配置 `pages.json` 首先,在 `pages.json` 文件中定义 TabBar 的结构和样式。以下是一个示例配置[^1]: ```json { "tabBar": { "color": "#666", "selectedColor": "#3cc51f", "backgroundColor": "#fff", "borderStyle": "black", "list": [ { "pagePath": "pages/index/index", "text": "首页", "iconPath": "static/tabbar/home.png", "selectedIconPath": "static/tabbar/home-active.png" }, { "pagePath": "pages/cart/cart", "text": "购物车", "iconPath": "static/tabbar/cart.png", "selectedIconPath": "static/tabbar/cart-active.png" }, { "pagePath": "pages/user/user", "text": "我的", "iconPath": "static/tabbar/user.png", "selectedIconPath": "static/tabbar/user-active.png" } ] }, "globalStyle": { "navigationBarTitleText": "全局标题", "navigationStyle": "custom" } } ``` - **`tabBar.list`**:定义每个 Tab 的页面路径、文本、图标路径等。 - **`globalStyle`**:设置全局的导航栏样式。 #### 2. 自定义 TabBar 如果需要更灵活的 TabBar 功能(如动态切换或隐藏),可以通过自定义 TabBar 实现。创建一个 Vue 组件作为自定义 TabBar,并在 `pages.json` 中注册它[^2]。 ##### 创建自定义 TabBar 组件 在 `components` 文件夹下新建一个文件 `customTabBar.vue`: ```vue <template> <view class="tab-bar"> <block v-for="(item, index) in tabBar.list" :key="index"> <view class="tab-bar-item" :class="{ active: index === currentIndex }" @click="switchTab(item, index)"> <image :src="currentIndex === index ? item.selectedIconPath : item.iconPath"></image> <text>{{ item.text }}</text> </view> </block> </view> </template> <script> export default { data() { return { currentIndex: 0, tabBar: { list: [ { pagePath: "pages/index/index", text: "首页", iconPath: "static/tabbar/home.png", selectedIconPath: "static/tabbar/home-active.png" }, { pagePath: "pages/cart/cart", text: "购物车", iconPath: "static/tabbar/cart.png", selectedIconPath: "static/tabbar/cart-active.png" }, { pagePath: "pages/user/user", text: "我的", iconPath: "static/tabbar/user.png", selectedIconPath: "static/tabbar/user-active.png" } ] } }; }, methods: { switchTab(item, index) { const url = '/' + item.pagePath; uni.switchTab({ url }); this.currentIndex = index; uni.setStorageSync('selectedIndex', index); // 异步保存当前选中的索引 } } }; </script> <style> .tab-bar { display: flex; justify-content: space-around; align-items: center; height: 100px; background-color: #fff; border-top: 1px solid #ccc; } .tab-bar-item { display: flex; flex-direction: column; align-items: center; justify-content: center; } .active { color: #3cc51f; } image { width: 40px; height: 40px; } text { font-size: 12px; } </style> ``` ##### 在 `pages.json` 中注册自定义 TabBar 将自定义 TabBar 注册到所有需要显示 TabBar页面中: ```json { "path": "pages/index/index", "style": { "navigationBarTitleText": "首页", "navigationStyle": "custom", "usingComponents": { "custom-tab-bar": "/components/customTabBar/customTabBar" } } } ``` #### 3. 全局调用 TabBar 为了让所有页面都带有 TabBar,可以在 `pages.json` 中为每个页面添加 `usingComponents` 属性,并确保页面路径在 `tabBar.list` 中定义[^3]。 #### 4. 处理全屏弹窗 TabBar 冲突 如果需要在 TabBar 页面中显示全屏弹窗,可以通过创建一个独立的页面来实现,并在 `pages.json` 中将其背景色设置为透明[^4]。例如: ```json { "path": "components/fullScreenPopup/fullScreenPopup", "style": { "navigationStyle": "custom", "app-plus": { "background": "transparent", "backgroundColor": "rgba(0,0,0,0)", "popGesture": "none" } } } ``` 然后通过 `uni.navigateTo` 打开该页面: ```javascript uni.navigateTo({ url: '/components/fullScreenPopup/fullScreenPopup' }); ``` ### 注意事项 - 确保 `tabBar.list` 中的 `pagePath` 和实际页面路径一致。 - 如果使用自定义 TabBar,需要手动处理页面跳转逻辑。 - 全屏弹窗页面应避免直接嵌套在 TabBar 页面中,以防止样式冲突。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值