微信小程序云开发3-云函数

1、新建云函数

2、在云函数文件夹右键选择-->在外部终端窗口中打开,输入命令安装依赖

 3、编辑云函数代码-->index.js文件

// index.js文件
// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

// 云函数入口函数
// async es7的异步
// event 接收前端传来的数据
exports.main = async (event, context) => {
  return event.a + event.b
}

 

4、上传云函数,上传后需等待显示上传成功

 

5、云函数本地调试-->右键文件夹选择开启云函数本地调试

点击调用开始调试云函数。

点击行号设置断点,F8继续执行。

 

6、前端调用云函数

// 在小程序前端调用云函数
  callyunfun(){
    wx.cloud.callFunction({
      // name 指定云函数名称
      name:'pagea',
      data:{
        a:3,
        b:5
      },
      success:(res)=>{
        console.log(res)
      },
      fail:(err)=>{
        console.log(err)
      }
    })
  }

 

posted @ 2021-01-08 14:21  RainstormDy  阅读(359)  评论(0)    收藏  举报