3.tabBar
3.1 什么是 tabBar
3.2 tabBar 的 6 个组成部分
3.3 tabBar 节点的配置项
属性 | 类型 | 必填 | 默认值 | 描述 |
---|---|---|---|---|
position | String | 否 | bottom | tabBar 的位置,仅支持 bottom/top |
borderStyle | String | 否 | black | tabBar 上边框的颜色,仅支持 black/white |
color | HexColor | 否 | tab 上文字的默认(未选中)颜色 | |
selectedColor | HexColor | 否 | tab 上的文字选中时的颜色 | |
backgroundColor | HexColor | 否 | tabBar 的背景色 | |
list | Array | 是 | tab 页签的列表,最少 2 个、最多 5 个 tab |
3.4 每个 tab 项的配置选项
属性 | 类型 | 必填 | 描述 |
---|---|---|---|
pagePath | String | 是 | 页面路径,页面必须在 pages 属性中预先定义(并且必须位于 pages 的前面部分) |
text | String | 是 | tab 上显示的文字 |
iconPath | String | 否 | 未选中时的图标路径;但当 postion 为 top 时,不显示 icon |
selectedIconPath | String | 否 | 选中时的图标路径;但当 postion 为 top 时,不显示 icon |
app.json:
json
{
"pages": [
"pages/home/home", //导航
"pages/message/message", //导航
"pages/contact/contact", //导航
"pages/index/index",
"pages/test/test",
"pages/logs/logs"
],
"tabBar": {
"list": [
{
//tabBar有很多属性,但是至少要包含一个list数组
"pagePath": "pages/home/home", //对应上面的home
"text": "首页",
"iconPath": "/images/tabs/home.png",
"selectedIconPath": "/images/tabs/home-active.png"
},
{
"pagePath": "pages/message/message", //对应上面的message
"text": "消息",
"iconPath": "/images/tabs/message.png",
"selectedIconPath": "/images/tabs/message-active.png"
},
{
"pagePath": "pages/contact/contact",
"text": "联系我们",
"iconPath": "/images/tabs/contact.png",
"selectedIconPath": "/images/tabs/contact-active.png"
}
]
},
"style": "v2",
"sitemapLocation": "sitemap.json"
}
4.案例:配置 tabBar
4.1 需求描述
4.2 实现步骤
4.3 实现
步骤 1 - 拷贝图标资源
步骤 2 - 新建 3 个对应的 tab 页面
步骤 3 - 配置 tabBar 选项
完整的配置代码
注:tabBar 的好处是,我们只需要配置,而不需要在 wxml 里面写任何内容,即可以实现导航栏!
四、页面配置
1. 页面配置文件的作用
小程序中,每个页面都有自己的 .json 配置文件,用来对当前页面的窗口外观、页面效果等进行配置。
2. 页面配置和全局配置的关系
3. 页面配置中常用的配置项
与全局的 window 配置类似
属性 | 类型 | 默认值 | 描述 |
---|---|---|---|
navigationBarBackgroundColor | HexColor | #000000 | 当前页面导航栏背景颜色,如 #000000 |
navigationBarTextStyle | String | white | 当前页面导航栏标题颜色,仅支持 black / white |
navigationBarTitleText | String | 当前页面导航栏标题文字内容 | |
backgroundColor | HexColor | #ffffff | 当前页面窗口的背景色 |
backgroundTextStyle | String | dark | 当前页面下拉 loading 的样式,仅支持 dark / light |
enablePullDownRefresh | Boolean | false | 是否为当前页面开启下拉刷新的效果 |
onReachBottomDistance | Number | 50 | 页面上拉触底事件触发时距页面底部距离,单位为 px |
message.json:
json
{
"usingComponents": {},
"navigationBarBackgroundColor": "#ff0000",
"navigationBarTextStyle": "black",
"navigationBarTitleText": "消息页面",
"backgroundColor": "#ff0000",
"backgroundTextStyle": "light",
"enablePullDownRefresh": true
}
五、网络数据请求
1.小程序中网络数据请求的限制
只能请求 https 的接口,意味着只能使用域名接口
2.配置 request 合法域名
3.发起 GET 请求
wx 对象:是一个顶级全局对象
例子:
js
// pages/home/home.js
Page({
/**
* 页面的初始数据
*/
data: {},
// 发起GET数据请求
getInfo() {
wx.request({
url: "https://www.escook.cn/api/get",
method: "GET",
data: {
name: "zs",
age: 20,
},
success: (res) => {
console.log(res.data); //res.data才是数据
},
});
},
// 发起POST请求
postInfo() {
wx.request({
url: "https://www.escook.cn/api/post",
method: "POST",
data: {
name: "ls",
age: 33,
},
success: (res) => {
console.log(res.data);
},
});
},
});
4.发起 POST 请求
5.在页面刚加载时请求数据——生命周期函数
js
// pages/home/home.js
Page({
/**
* 页面的初始数据
*/
data: {},
// 发起GET数据请求
getInfo() {
wx.request({
url: "https://www.escook.cn/api/get",
method: "GET",
data: {
name: "zs",
age: 20,
},
success: (res) => {
console.log(res.data);
},
});
},
// 发起POST请求
postInfo() {
wx.request({
url: "https://www.escook.cn/api/post",
method: "POST",
data: {
name: "ls",
age: 33,
},
success: (res) => {
console.log(res.data);
},
});
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
this.getInfo();
this.postInfo();
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {},
});
6.跳过 request 合法域名校验
只有在开发阶段可以用 http,部署了还是必须要用 https!
7.关于跨域和 Ajax 的说明
小程序不叫 ajax 请求,而是网络数据请求!
六、案例 - 本地生活
1.首页效果以及实现步骤
2.接口地址
3.具体实现
3.1 全局配置
app.json
json
{
"pages": [
"pages/home/home",
"pages/message/message",
"pages/contact/contact" //联系我们
],
"window": {
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#2b4b6b",
"navigationBarTitleText": "本地生活",
"navigationBarTextStyle": "white"
},
"tabBar": {
"list": [
{
"pagePath": "pages/home/home",
"text": "首页",
"iconPath": "/images/tabs/home.png",
"selectedIconPath": "/images/tabs/home-active.png"
},
{
"pagePath": "pages/message/message",
"text": "消息",
"iconPath": "/images/tabs/message.png",
"selectedIconPath": "/images/tabs/message-active.png"
},
{
"pagePath": "pages/contact/contact",
"text": "联系我们",
"iconPath": "/images/tabs/contact.png",
"selectedIconPath": "/images/tabs/contact-active.png"
}
]
},
"style": "v2",
"sitemapLocation": "sitemap.json"
}
3.2 具体页面
home.wxml
html
<!--pages/home/home.wxml-->
<!-- 轮播图区域,indicator-dots circular属性-->
<swiper indicator-dots circular>
<swiper-item wx:for="{{swiperList}}" wx:key="id">
<image src="{{item.image}}"></image>
</swiper-item>
</swiper>
<!-- 九宫格区域 -->
<view class="grid-list">
<view class="grid-item" wx:for="{{gridList}}" wx:key="id">
<image src="{{item.icon}}"></image>
<text>{{item.name}}</text>
</view>
</view>
<!-- 图片区域 -->
<view class="img-box">
<image src="/images/link-01.png" mode="widthFix"></image>
<image src="/images/link-02.png" mode="widthFix"></image>
</view>
home.wxss
css
/* pages/home/home.wxss */
swiper {
height: 350rpx;
}
swiper image {
width: 100%;
height: 100%;
}
.grid-list {
/*外层*/
display: flex;
flex-wrap: wrap;
border-left: 1rpx solid #efefef;
border-top: 1rpx solid #efefef;
}
.grid-item {
/*内层*/
width: 33.33%;
height: 200rpx;
display: flex; /*伸缩盒布局,为了让内容方便布局*/
flex-direction: column; /*纵向布局*/
align-items: center; /*居中*/
justify-content: center; /*居中*/
border-right: 1rpx solid #efefef; /*右侧边框*/
border-bottom: 1rpx solid #efefef;
box-sizing: border-box; /*改成怪异盒模型*/
}
.grid-item image {
width: 60rpx;
height: 60rpx;
}
.grid-item text {
font-size: 24rpx;
margin-top: 10rpx;
}
.img-box {
display: flex;
padding: 20rpx 10rpx;
justify-content: space-around; /*把10%的距离平分*/
}
.img-box image {
width: 45%; /*让其留出10%的距离*/
}
home.js
js
// pages/home/home.js
Page({
/**
* 页面的初始数据
*/
data: {
// 存放轮播图数据的列表
swiperList: [],
// 存放九宫格数据的列表
gridList: [],
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
this.getSwiperList();
this.getGridList();
},
// 获取轮播图数据的方法
getSwiperList() {
wx.request({
url: "https://www.escook.cn/slides",
method: "GET",
success: (res) => {
this.setData({
swiperList: res.data,
});
},
});
},
// 获取九宫格数据的方法
getGridList() {
wx.request({
url: "https://www.escook.cn/categories",
method: "GET",
success: (res) => {
this.setData({
gridList: res.data,
});
},
});
},
});
总结