小程序(三)配置tabbar及自定义tabbar样式 您所在的位置:网站首页 微信小程序左对齐怎么弄 小程序(三)配置tabbar及自定义tabbar样式

小程序(三)配置tabbar及自定义tabbar样式

2023-10-09 19:54| 来源: 网络整理| 查看: 265

关于tabbar部分,官方文档是有明确的说明的,当然,我这里是不存在把官方文档给你复制一遍的情况。我大概把我再看官方文档过程中遇到的坑,大概复述一下。

一:配置tabbar

这个主要是使用小程序自带的tabbar,在项目根目录下的app.json中配置,这个简单配置一下就可以了。

这里放一下我的app.json配置:

{ "pages": [ "pages/index/index", "pages/logs/logs", "pages/article/article", "pages/board/board", "pages/remark/remark" ], "tabBar": { "selectedColor": "#31869B", "list": [ { "current": 0, "pagePath": "pages/index/index", "text": "首页", "iconPath": "/images/index-select.png", "selectedIconPath": "/images/index-selected.png" }, { "current": 1, "pagePath": "pages/remark/remark", "text": "随言碎语", "iconPath": "/images/sui-select.png", "selectedIconPath": "/images/sui-selected.png" }, { "current": 2, "pagePath": "pages/board/board", "text": "留言板", "iconPath": "/images/board-select.png", "selectedIconPath": "/images/board-selected.png" } ] }, "usingComponents": { "weuitabbar": "/miniprogram_npm/weui-miniprogram/tabbar/tabbar" }, "window": { "backgroundTextStyle": "light", "navigationBarBackgroundColor": "#fff", "navigationBarTitleText": "WeChat", "navigationBarTextStyle": "black" }, "style": "v2", "sitemapLocation": "sitemap.json" }

更详细的tabbar配置属性说明,请移步官方文档《tabbar》

最后效果如下图所示:

在这里插入图片描述

二:自定义tabbar

有些时候,官方的样式并不能满足我们的要求,这个时候,我们就需要自定义tabbar的样式,当然,这个官方也是有说明的,只是,说明比较简单,官方给了一段示例代码。具体请移步官方文档《自定义tabbar》

然后,我这里大概分享下我是怎么写的:

App.json中,tabbar有一个custom属性,这个很重要,如果将这个属性设置为true,那么程序会默认使用自定义的tabbar。

那么我的app.json中的tabbar部分代码,就变成了下面的这个样子:

"tabBar": { "custom": true, },

在小程序根目录下创建目录custom-tab-bar,名字一定要是这个,别瞎起名。

在目录中创建组件index,注意,这里一定要注意,这里创建的是组件,而不是页面,虽然他们都是四个文件,但是他们是完全不一样的。

创建成功之后,文件目录如下图所示:

在这里插入图片描述

然后,我们开始编写四个文件中的代码:

(1):index.js

Component({ data: { selected: 0, color: "#7A7E83", selectedColor: "#31869B", fontWeight:'bold', "list": [ { "current": 0, "pagePath": "/pages/index/index", "text": "首页", "iconPath": "/images/index-select.png", "selectedIconPath": "/images/index-selected.png" }, { "current": 1, "pagePath": "/pages/remark/remark", "text": "随言碎语", "iconPath": "/images/sui-select.png", "selectedIconPath": "/images/sui-selected.png" }, { "current": 2, "pagePath": "/pages/board/board", "text": "留言板", "iconPath": "/images/board-select.png", "selectedIconPath": "/images/board-selected.png" } ] }, attached() { }, methods: { switchTab(e) { console.log(e); const idx = e.currentTarget.dataset.index const path = e.currentTarget.dataset.path // this.setData({ // selected: idx // }) wx.switchTab({ url: path, }); } } })

(2):index.json

这里边本来我打算用weui的tabbar组件来着,但是目前还没玩明白,这个回头再说。

{ "component": true, "usingComponents": { "weuitabbar": "/miniprogram_npm/weui-miniprogram/tabbar/tabbar" } }

(3):index.wxml



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有