一、页面跳转到前一页,不会刷新页面,只会更改响应的变量
let Pages = getCurrentPages();//获取当前页面的页面栈
let PrevPage = Pages[Pages.length - 2];//获取上级页面的page对象
PrevPage.setData({
inputvalue: content,// inputvalue前一页数据
},function(){
wx.navigateBack({
delta: 1
});
});
二、页面跳转
1.navigateTo, redirectTo 只能打开非 tabBar 页面。
2.switchTab 只能打开 tabBar 页面,不带参数。
3. reLaunch 关闭所有页面,打开到应用内的某个页面。
4.页面底部的 tabBar 由页面决定,即只要是定义为 tabBar 的页面,底部都有 tabBar。
5.调用页面路由带的参数可以在目标页面的onLoad中获取。
三、本地存储
1. wx.setStorageSync('storeId', storeId);
2. var storeId = wx.getStorageSync('storeId');
四、手机号正则判断
if (!(/^1[34578]\d{9}$/.test(tel))) {
wx.showModal({
title: '提示',
content: '手机号码有误,请重填',
})
}
五、html里面写小于,大于,之类的需要用这种格式{{’<’}},不能用html里的>
六、小程序.wxml文件中打空格:输入法选择中文+全角+再按空格
七、判断是一个空对象:JSON.stringify(options) == “{}”
八、小程序获取定位等需在app.json中写
"permission": {
"scope.userLocation": {
"desc": "你的位置信息将用于小程序位置接口的效果展示"
}
}
九、app.json中tabBar的写法
"tabBar": {
"color": "#000",
"selectedColor": "#9cc829",
"borderStyle": "black",
"backgroundColor": "#fff",
"list": [
{
"pagePath": "pages/index/index",
"text": "首页",
"iconPath": "images/home.png",
"selectedIconPath": "images/home_active.png"
},
{
"pagePath": "pages/catalog/catalog",
"text": "分类",
"iconPath": "images/catalog.png",
"selectedIconPath": "images/catalog_active.png"
},
{
"pagePath": "pages/cart/cart",
"text": "购物车",
"iconPath": "images/cart.png",
"selectedIconPath": "images/cart_active.png"
},
{
"pagePath": "pages/mine/mine",
"text": "我的",
"iconPath": "images/mine.png",
"selectedIconPath": "images/mine_active.png"
}
]
}
十、拼接options里的参数
objectToUrlParams: function (t) {
var e = "";
for (var r in t) e += "&" + r + "=" + t[r];
return e.substr(1);
}
wx.redirectTo({
url: '/' + t.route + "?" + that.objectToUrlParams(t.options)
})
十一、在wxml里面进行对数字进行保留2位小数
1.新建一个filter.wxs文件
var filters = {
toFix: function (value) {
return value.toFixed(2)//此处2为保留两位小数,保留几位小数,这里写几
}
}
module.exports = {
toFix: filters.toFix
}
2.在需要用到的地方引入,注意目录结构
<wxs module="filters" src="../../utils/filter.wxs"></wxs>
3.在wxml里面调用
<text>¥{{filters.toFix(checkedGoodsAmount)}}</text>
十二、获取当前位置经纬度以及获取定位所在地
wx.getSetting({
success(res) {
if (!res.authSetting['scope.userLocation']) {
wx.authorize({
scope: 'scope.userLocation',
success() {
// 用户已经同意小程序使用定位功能
wx.getLocation({
type: 'wgs84',
success(res) {
const latitude = res.latitude
const longitude = res.longitude
const speed = res.speed
const accuracy = res.accuracy
//如果需要获取相应所在定位地理位置名,需把经纬度发给百度地图请求查询
wx.request({
url: 'https://api.map.baidu.com/geocoder/v2/?ak=c7QlcIklxGgxgLm4YXVo0GNUWFu8gueU&location=' + latitude + ',' + longitude + '&output=json',
data: {},
header: {
'Content-Type': 'application/json'
},
success: function (res) {
// success
console.log(res);
var city = res.data.result.addressComponent.city;
that.setData({ city: city });
},
fail: function () {
// that.setData({ city: "获取定位失败" });
},
})
}
})
},
fail: function(res2) {
console.log(res2)
wx.showModal({
title: '温馨提示',
content: '您拒绝授权后,将无法获取定位',
cancelText: '去授权',
success: function(res) {
if (res.confirm) {
} else if (res.cancel) {
that.setData({
locationShow: true
})
}
}
})
}
})
}else{
// wx.getLocation开始的部分
}
}
})