微信小程序记录 - api

 

 

     
wx.canIUse(string schema)  当前版本是否可用    返回值 boolean   
     
     
     
     
wx.switchTab({
  url: '/index'
})
跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面  
wx.reLaunch({
  url: 'test?id=1'
})
关闭所有页面,打开到应用内的某个页面  
wx.redirectTo({
  url: 'test?id=1'
})
关闭当前页面,跳转到应用内的某个页面。但是不允许跳转到 tabbar 页面。  
wx.navigateTo({
  url: 'test?id=1'
})
保留当前页面,跳转到应用内的某个页面。但是不能跳到 tabbar 页面。使用 wx.navigateBack 可以返回到原页面。小程序中页面栈最多十层。  
Page({
  onLoad(option) {
    console.log(option.query)
  }
})
获取路由参数  
wx.navigateBack({
  delta: 2
})
关闭当前页面,返回上一页面或多级页面。可通过 getCurrentPages 获取当前的页面栈,决定需要返回几层。  
wx.showToast({
  title: '成功',
  icon: 'success',
  duration: 2000
})
显示消息提示框  
wx.showModal({
  title: '提示',
  content: '这是一个模态弹窗',
  success(res) {
    if (res.confirm) {
      console.log('用户点击确定')
    } else if (res.cancel) {
      console.log('用户点击取消')
    }
  }
})
显示模态对话框  
wx.showLoading({
  title: '加载中',
})

setTimeout(function () {
  wx.hideLoading()
}, 2000)
等待框  
wx.showActionSheet({
  itemList: ['A', 'B', 'C'],
  success(res) {
    console.log(res.tapIndex)
  },
  fail(res) {
    console.log(res.errMsg)
  }
})
操作菜单  

wx.hideToast(Object object)

 

隐藏消息提示框  

wx.hideLoading(Object object)

隐藏 loading 提示框  
Page({
  onPullDownRefresh() {
    wx.stopPullDownRefresh()
  }
})
下拉刷新  
wx.startPullDownRefresh()
停止下拉刷新

 

 

 

 

 

 

wx.pageScrollTo({
  scrollTop: 0,
  duration: 300
})
将页面滚动到目标位置  

wx.createAnimation(Object object)

创建一个动画实例 animation。调用实例的方法来描述动画。最后通过动画实例的 export 方法导出动画数据传递给组件的 animation 属性。

duration(ms)

timingFunction

(linear)(ease)(ease-in)

('ease-out')

delay

transformOrigin

Animation.export()

导出动画队列。export 方法每次调用后会清掉之前的动画操作。  

Animation.step(Object object)

 

表示一组动画完成。可以在一组动画中调用任意多个动画方法,一组动画中的所有动画会同时开始,一组动画完成后才会进行下一组动画。  

Animation.opacity(number value)

 

设置透明度  

Animation.width(number|string value)

 

设置宽度  
     
     

 

<view
  animation="{{animationData}}"
  style="background:red;height:100rpx;width:100rpx"
></view>
例子:
Page({
  data: {
    animationData: {}
  },
  onShow() {
    const animation = wx.createAnimation({
      duration: 1000,
      timingFunction: 'ease',
    })
    this.animation = animation
    animation.scale(2, 2).rotate(45).step()
    this.setData({
      animationData: animation.export()
    })
})

wx.nextTick(function callback)

延迟一部分操作到下一个时间片再执行。(类似于 setTimeout)  
Component({
  doSth() {
    this.setData({number: 1}) // 直接在当前同步流程中执行

    wx.nextTick(() => {
      this.setData({number: 3}) // 在当前同步流程结束后,下一个时间片执行
    })

    this.setData({number: 2}) // 直接在当前同步流程中执行
  }
})

wx.onWindowResize

(function callback)

监听窗口尺寸变化事件监听窗口尺寸变化事件  

wx.offWindowResize(function callback)

取消监听窗口尺寸变化事件  
     
     
     
     
     
     
     
     
     
     
     

wx.request(Object object)

发起请求

wx.request({
  url: 'test.php', // 仅为示例,并非真实的接口地址
  data: {
    x: '',
    y: ''
  },
  header: {
    'content-type': 'application/json' // 默认值
  },
  success(res) {
    console.log(res.data)
  }
})

RequestTask.abort()

中断请求任务

 

const requestTask = wx.request({
  url: 'test.php', // 仅为示例,并非真实的接口地址
  data: {
    x: '',
    y: ''
  },
  header: {
    'content-type': 'application/json'
  },
  success(res) {
    console.log(res.data)
  }
})
requestTask.abort() // 取消请求任务

 

wx.downloadFile(Object object)

下载文件资源到本地。客户端直接发起一个 HTTPS GET 请求,返回文件的本地临时路径

wx.downloadFile({
  url: 'https://example.com/audio/123', // 仅为示例,并非真实的资源
  success(res) {
    // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
    if (res.statusCode === 200) {
      wx.playVoice({
        filePath: res.tempFilePath
      })
    }
  }
})

DownloadTask

一个可以监听下载进度变化事件,以及取消下载任务的对象

const downloadTask = wx.downloadFile({
  url: 'http://example.com/audio/123', // 仅为示例,并非真实的资源
  success(res) {
    wx.playVoice({
      filePath: res.tempFilePath
    })
  }
})

downloadTask.onProgressUpdate((res) => {
  console.log('下载进度', res.progress)
  console.log('已经下载的数据长度', res.totalBytesWritten)
  console.log('预期需要下载的数据总长度', res.totalBytesExpectedToWrite)
})

downloadTask.abort() // 取消下载任务

wx.uploadFile(Object object)

将本地资源上传到服务器。客户端发起一个 HTTPS POST 请求,其中 content-type 为 multipart/form-data。使用前请注意阅读相关说明

wx.chooseImage({
  success(res) {
    const tempFilePaths = res.tempFilePaths
    wx.uploadFile({
      url: 'https://example.weixin.qq.com/upload', // 仅为示例,非真实的接口地址
      filePath: tempFilePaths[0],
      name: 'file',
      formData: {
        user: 'test'
      },
      success(res) {
        const data = res.data
        // do something
      }
    })
  }
})

UploadTask

一个可以监听上传进度变化事件,以及取消上传任务的对象

const uploadTask = wx.uploadFile({
  url: 'http://example.weixin.qq.com/upload', // 仅为示例,非真实的接口地址
  filePath: tempFilePaths[0],
  name: 'file',
  formData: {
    user: 'test'
  },
  success(res) {
    const data = res.data
    // do something
  }
})

uploadTask.onProgressUpdate((res) => {
  console.log('上传进度', res.progress)
  console.log('已经上传的数据长度', res.totalBytesSent)
  console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
})

uploadTask.abort() // 取消上传任务

sync:同步

wx.setStorageSync(string key, any data)

wx.setStorage(string key, any data)

wx.setStorage({
  key: 'key',
  data: 'value'
})
try {
  wx.setStorageSync('key', 'value')
} catch (e) { }

wx.removeStorageSync(string key)

wx.removeStorage(string key)

wx.removeStorage({
  key: 'key',
  success(res) {
    console.log(res)
  }
})
try {
  wx.removeStorageSync('key')
} catch (e) {
  // Do something when catch error
}

wx.getStorageSync(string key)

wx.getStorage(Object object)

wx.getStorage({
  key: 'key',
  success(res) {
    console.log(res.data)
  }
})
try {
  const value = wx.getStorageSync('key')
  if (value) {
    // Do something with return value
  }
} catch (e) {
  // Do something when catch error
}

 

Object wx.getStorageInfoSync()

Object wx.getStorageInfo()

wx.getStorageInfo({
  success(res) {
    console.log(res.keys)
    console.log(res.currentSize)
    console.log(res.limitSize)
  }
})
try {
  const res = wx.getStorageInfoSync()
  console.log(res.keys)
  console.log(res.currentSize)
  console.log(res.limitSize)
} catch (e) {
  // Do something when catch error
}

wx.clearStorage(Object object)

wx.clearStorage()
清理本地数据缓存
try {
  wx.clearStorageSync()
} catch (e) {
  // Do something when catch error
}

上拉加载: onReachBottom

 

 

发布了40 篇原创文章 · 获赞 22 · 访问量 8283
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章