• 注册
当前位置:1313e > 小程序 >正文

小程序常用API介绍

小程序常用API接口

  1. wx.request https网络请求

wx.request({

url: 'test.php', //仅为示例,并非真实的接口地址

method:"GET",

data: {

x: '' ,

y: ''

},

header: {

'content-type': 'application/json' },

success: function(res) {

console.log(res.data)

}

})

  1. 本地缓存

 

  1. 通过key的形式添加缓存setStorage (异步接口)

wx.setStorage({

key:"key"

data:"value"

})

  1. 通过key的形式获取缓存getStorage (异步接口)

wx.getStorage({

key: 'key',

success: function(res) {

console.log(res.data) }

})

  1. 从本地缓存中异步移除指定 key

wx.removeStorage({

key: 'key',

success: function(res) {

console.log(res.data)

}

})

  1. 清理本地数据缓存

wx.clearStorage()

  1. 显示、隐藏消息提示框

wx.showToast({

title: '加载中',

icon: 'loading',

duration: 10000 })

setTimeout(function(){

wx.hideToast()

},2000)

  1. 动态设置当前页面的标题

wx.setNavigationBarTitle({

title: '当前页面'

})

  1. 导航

    1. 保留当前页面,跳转到应用内的某个页面

wx.navigateTo({

url: 'test?id=1'

})

  1. 关闭当前页面,跳转到应用内的某个页面

wx.redirectTo({ url: 'test?id=1'

})

  1. 获取用户信息,需要先调用wx.login 接口

wx.getUserInfo({

success: function(res) {

var userInfo = res.userInfo

var nickName = userInfo.nickName

var avatarUrl = userInfo.avatarUrl

var gender = userInfo.gender //性别 0:未知、1:男、2:女

var province = userInfo.province

var city = userInfo.city

var country = userInfo.country

}

})

  1. 获取系统信息(异步接口)

wx.getSystemInfo({

success: function(res) {

console.log(res.model)

console.log(res.pixelRatio)

console.log(res.windowWidth)

console.log(res.windowHeight)

console.log(res.language)

console.log(res.version)

}

})

  1. 拨打电话

wx.makePhoneCall({

phoneNumber: '1340000' //仅为示例,并非真实的电话号码

}

 

  1. 获取当前的地理位置、速度

wx.getLocation({

type: 'wgs84',

success: function(res) {

var latitude = res.latitude

var longitude = res.longitude

var speed = res.speed

var accuracy = res.accuracy

}

})

  1. 重要的 var that=this

原来我们忘记了javascript 语言中 this关键字的用法了。在javascript语言中,this代表着当前的对象,它在程序中随着执行的上下文随时会变化。在本例中回调函数对象相对于showactionsheet点击事件函数对象已经发生了变化。所以已经不是原来的页面对象了。自然就没有了data属性,也没有了data.itemLists属性了。解决的办法就是复制一份当前的对象。所以我们有了这个重要的语句:

var that=this;//this对象复制到临时变量that.

这时候我们使用that 就不会找不到原来的对象了。

console.log(that.data.itemLists[res.tapIndex]);//使用that.data 属性

 

文章来源:刘俊涛的博客

地址:http://www.cnblogs.com/lovebing

欢迎关注,有问题一起学习欢迎留言、评论。

转载于:https://www.cnblogs.com/lovebing/p/8794538.html

本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 162202241@qq.com 举报,一经查实,本站将立刻删除。

最新评论

欢迎您发表评论:

请登录之后再进行评论

登录