API就是微信提供的一些功能接口,可以在js文件中直接调用,实现特定功能。
1.路由
● wx.navigateTo:保留当前页面,跳转到应用内的某个页面,不能跳转到tabbar页面。
● wx.navigateBack:可以返回到原页面,小程序中页面栈最多十层。
● wx.switchTab:跳转到tabbar页面,关闭其他页面。
● wx.redirectTo:关闭当前页面,跳转到应用内的某个页面。但是不允许跳转到 tabbar 页面。
# api.wxml
<!--
wx.navigateTo
跳转页面的两种方式
1.使用 navigator 组件
2.使用 wx.navigateTo
-->
<view>wx.navigateTo</view>
<navigator url="/pages/event/event?id=1001&name=lose">跳转到event</navigator>
<button type="primary" bindtap="goEvent">跳转到event</button>
<navigator url="/pages/index/index" open-type="switchTab">跳转到index</navigator>
<button type="primary" bindtap="goIndex">跳到index</button>
# api.js
Page({
goEvent(){
wx.navigateTo({
url: '/pages/event/event?id=1002&name=leo'
})
},
goIndex(){
wx.switchTab({
url: '/pages/index/index' // 路径后不能带参数
})
}
})
# event.js
Page({
/**
* 获取参数信息
*/
onLoad:function(options){
// 参数options中包含了跳转到当前页面时,所携带的参数信息
console.log(options)
}
})
2.网络
wx.request:发起 HTTPS网络请求,就是ajax请求,小程序为保证请求的安全性,要求所有被请求的地址都必须是在合法的域名列表中,由开发者自行配置,微信进行审核,请求地址必须是HTTPS协议(加密、安全)。
在开发环境下可以关闭合法校验(详情 -> 本地设置 -> 不校验合法域名…),该方式只有在微信开发者工具下有效,在生产环境或微信中预览无效。
<view class="title">wx.request</view>
<button type="primary" bindtap="sendAjax">发送Ajax请求</button>
// pages/api/api.js
Page({
sendAjax(){
wx.request({
url: 'https://m.douban.com/rexxar/api/v2/subject_collection/movie_showing/items',
method: "GET"