1.http
wx.request(OBJECT);
例子:
wx.request( {
url: "http://op.juhe.cn/onebox/weather/query",
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
method: "POST",
//data: { cityname: "上海", key: "1430ec127e097e1113259c5e1be1ba70" },
data: Util.json2Form( { cityname: "上海", key: "1430ec127e097e1113259c5e1be1ba70" }),
complete: function( res ) {
that.setData( {
toastHidden: false,
toastText: res.data.reason,
city_name: res.data.result.data.realtime.city_name,
date: res.data.result.data.realtime.date,
info: res.data.result.data.realtime.weather.info,
});
if( res == null || res.data == null ) {
console.error( '网络请求失败' );
return;
}
}
})
2.路由
2.1 保留当前页面,跳转到应用内的某个页面,使用 wx.navigateBack 能够返回到原页面。
// 注意:目前页面路径最多只能十层。
wx.navigateTo(OBJECT)
// 示例 1
wx.navigateTo({
url: 'test?id=1'
})
//test.js
Page({
onLoad: function(option){
console.log(option.query)
}
})
// 示例二
wx.navigateTo({
url:"pages/home/home?type=2"
});
// 而后在 home.js 中的 onLoad() 函数中获得值:option.type 就能够获得了,以下:
onLoad: function (option) {
this.setData({
type:option.type,
});
console.log(option.type);
}
2.2 关闭当前页面,跳转到应用内的某个页面。
wx.redirectTo(OBJECT)
// 示例
wx.redirectTo({
url: 'test?id=1'
})
//test.js
Page({
onLoad: function(option){
console.log(option.query)
}
})
// wx.navigateTo() 是保留当前页面,跳转到某个页面,跳转页面后能够返回上一页。
// wx.redirectTo() 是关闭当前页面,跳转到某个页面,跳转页面后不能返回上一页。
// 等等
3.全局方法和数据
// 全局的 getApp() 函数能够用来获取到小程序实例。
// other.js
var appInstance = getApp()
console.log(appInstance.globalData) // I am global data
4.数据和事件
4.1 改变数据
this.setData({
message:"你好 米娜!"
})
4.2 获取数据
this.data.message
//重点,若要用传参的形式设置数组的值,要用[]将字符串参数值括起来
//示例一
Page({
data: {
plaintPerList: []
}
})
var type = 'plaintPerList';
var list = [];
this.setData({
[type]: list
})
//示例二
var printPrice = "item["+i+"].print_price";
this.setData({
[printPrice]: e.detail.value
4.3 经过事件传递数据 dataset
// target 触发事件的源组件
// currentTarget 事件绑定的当前组件
//-----wxml----
<view class="weui-cell__bd">
<input class="weui-input" value="{{item.name}}" placeholder="请输入姓名" bindinput="saveEditor"
data-editor-Type="plaintPerList" data-json-Key="name" data-editor-Index="{{index}}"/>
</view>
//-----js----
saveEditor: function(e) {
// type 数组对象名称
// index 位置
// jsonKey 数组属性名
var that = this;
var type = e.currentTarget.dataset.editorType,
index = e.currentTarget.dataset.editorIndex,
jsonKey = e.currentTarget.dataset.jsonKey;
var list = that.data[type];
list[index][jsonKey] = e.detail.value;
// 从新赋值
that.setData({
[type]: list
})
}