动态绑定数据、三元运算、算数运算、事件绑定
<!-- 对比和 Vue 的差异 -->
<image src="{{ imgSrc }}" mode="widthFix"></image>
e.target:点击谁就是谁
e.currentTarget:谁绑定就是谁
setData
函数用于将数据从逻辑层发送到视图层(异步),同时改变对应的 this.data
的值(同步),可以配合事件绑定的参数传递来证明
<view>
<view> {{count}} </view>
<button type="primary" bindtap="btnTapHandler" data-info="{{count}}">按钮</button>
</view>
btnTapHandler(e) {
this.setData({
count: this.data.count + 1
})
console.log(this.data.count) // 同步
console.log(e.target.dataset.info) // 异步
}
实现双向数据绑定
<!-- 注意结束标签 -->
<view> {{msg}} </view>
<input value="{{msg}}" type="text" bindinput="handleInput" />
handleInput(e) {
this.setData({
msg: e.detail.value
})
}
创建和销毁,显示与隐藏
<!-- 默认是 index 和 item -->
<view wx:for="{{arr}}" wx:for-index="idx" wx:for-item="itemName" wx:key="idx"> {{idx}}: {{itemName}} </view>
不同设备屏幕统一等分为 750 份, 1 份就是 1 rpx, 在较小的设备上,1 rpx 所代表的宽度较小,在较大的设备上,1 rpx 所代表的宽度较大,小程序会自动把 rpx 的样式单位换算成对应的像素单位来渲染,从而实现屏幕适配!
"window":{
// 标题文字
"navigationBarTitleText": "黑马程序员",
// 标题背景色,只支持 16 进制
"navigationBarBackgroundColor": "#2b4b6b",
// 标题文字颜色,只支持 black 和 white
"navigationBarTextStyle":"white",
// 开启下拉刷新时 loading 的颜色,支持 dark 和 light
"backgroundTextStyle":"dark",
// 开启下拉刷新
"enablePullDownRefresh": true,
// 下拉刷新后看到的背景色
"backgroundColor": "#efefef",
// 上滑触底的距离,默认 50px
"onReachBottomDistance": 50
}
backgroundColor: tabBar 的背景色
iconPath: 未选中的图片路径
selectedIconPath: 选中的图片路径
borderStyle: tabBar 上边框的颜色
color: 文字颜色
selectedColor: 选中时的文字颜色
"tabBar": {
"list": [
{
// 页面路径,必须在 pages 中预先定义
"pagePath": "pages/index/index",
// tab 上显示的文字
"text": "首页",
// 未选中时的图标路径,当 postion 为 top 时,不显示 icon
"iconPath": "",
// 选中时的图标路径,当 postion 为 top 时,不显示 icon
"selectedIconPath": ""
},
{
"pagePath": "pages/test/test",
"text": "测试",
"iconPath": "",
"selectedIconPath": ""
}
]
}
navigationBarBackgroundColor: 导航背景颜色
navigationBarTextStyle: 标题颜色
navigationBarTitleText: 标题内容
backgroundColor: 背景色,即下拉刷新时期望看到的背景色
backgroundTextStyle: 下拉 loading 的样式
enablePullDownRefresh: 是否开启下拉刷新
onReachBottomDistance: 上滑加载距离
两个限制:安全考虑,只能请求 HTTPS 类型的接口;必须将接口的域名添加到信任列表中,域名只支持 https 协议,不能使用 IP 地址或 localhost,ICP 备案,一个月内最多可申请 5 次修改!
onLoad: function (options) {
wx.request({
url: 'https://www.escook.cn/api/get',
method: 'GET',
data: {
name: 'zs',
age: 22
},
success: (res) => {
console.log(res)
}
});
}
a. 安装
npm init -y
npm install --save miniprogram-api-promise
b. 配置
app.js
import { promisifyAll } from 'miniprogram-api-promise';
wx.p = {};
promisifyAll(wx, wx.p);
c. 使用
test.js
wx.p.request({
url: 'https://www.escook.cn/api/get',
method: 'GET',
data: {
name: 'zs',
age: 22,
},
})
.then((r) => {
console.log(r);
});
{
"window":{
// 开启下拉刷新时 enablePullDownRefresh,loading 的颜色
"backgroundTextStyle":"light",
// 导航的背景色
"navigationBarBackgroundColor": "#2b4b6b",
// 导航的文本
"navigationBarTitleText": "本地生活",
// 导航的文本颜色
"navigationBarTextStyle":"white"
},
}
{
"tabBar": {
"list": [{
// TabBar 对应的页面路径
"pagePath": "pages/home/home",
// TabBar 文本
"text": "首页",
// TabBar 图标
"iconPath": "/images/tabs/home.png",
// TabBar 选中的图标
"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"
}]
},
}
Page({
data: {
swiperList: [],
},
onLoad: function (options) {
this.getSwiperList();
},
getSwiperList() {
wx.request({
url: 'https://www.escook.cn/slides',
success: (res) => {
this.setData({
swiperList: res.data,
});
},
});
},
});
<swiper indicator-dots circular>
<swiper-item wx:for="{{swiperList}}" wx:key="id">
<image src="{{item.image}}"></image>
</swiper-item>
</swiper>
swiper {
height: 350rpx;
}
swiper image {
width: 100%;
height: 100%;
}
Page({
data: {
gridList: [],
},
onLoad: function (options) {
this.getGridList();
},
getGridList() {
wx.request({
url: 'https://www.escook.cn/categories',
success: (res) => {
this.setData({
gridList: res.data,
});
},
});
},
});
<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>
.grid-list {
display: flex;
flex-wrap: wrap;
border-top: 1rpx solid #efefef;
border-left: 1rpx solid #efefef;
}
.grid-item {
width: 33.33%;
height: 200rpx;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
border-bottom: 1rpx solid #efefef;
box-sizing: border-box;
}
/* 小技巧 */
.grid-item:not(:nth-child(3n)) {
border-right: 1rpx solid #efefef;
}
.grid-item image {
width: 60rpx;
height: 60rpx;
}
.grid-item text {
margin-top: 10rpx;
font-size: 24rpx;
}
<view class="img-box">
<image src="/images/link-01.png" mode="widthFix"></image>
<image src="/images/link-02.png" mode="widthFix"></image>
</view>
.img-box {
display: flex;
padding: 20rpx 10rpx;
justify-content: space-around;
}
.img-box image {
width: 45%;
}
消息队列(Message Queue)是一种使用高效可靠的数据传输机制来进行平台无关的数...
什么是 AJAX AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XM...
经过两个月的学习终于到了考完PMP的这一天。我决定要说说这两个月的考试心得。 ...
我们知道在PHP4里面,当我们使用一个类的时候,必须将该类所以文件使用include或...
系列前言 本系列博文仅为博主个人学习笔记通过博客理清学习思路用于复习。如有记...
本文实例讲述了JSP中使用JSTL按不同条件输出内容的方法。分享给大家供大家参考。...
复制代码 代码如下: title=request("title") title=replace(title,"chr(34)","""...
Base64编码,是我们程序开发中经常使用到的编码方法。它是一种基于用64个可打印...
一、ERR对象 err对象使用的时候不需要创建实例,就是说你要用的时候随便拿来使用,...
PureStudy:学科知识分享——个人网站开发全解 项目描述 GITHUB链接 : https://...