小程序

微信小程序实战开发 | 小程序

2020-01-08  本文已影响0人  LM林慕

此文项目代码:https://github.com/bei-yang/I-want-to-be-an-architect
码字不易,辛苦点个star,感谢!

引言

此篇文章主要涉及以下内容:

  1. 小程序开发环境搭建
  2. 小程序项目结构
  3. 小程序基础知识
  4. 云开发实战
  5. 如何使用云存储
  6. 如何使用云数据库新增数据
  7. 如何查询云数据库的数据
  8. 开发登录云函数
  9. 开发商品列表

此篇文章目标:

  1. 注册自己的小程序开发账号
  2. 能够自己开发简易小程序页面
  3. 能够说出云开发和普通后台开发的区别
  4. 开发自己的云函数
  5. 开发自己的全栈小程序

注册账号

官网地址:https://mp.weixin.qq.com/

image.png

和注册一个普通账号一样,不在赘述。

开发者工具

  1. 下载安装:https://developers.weixin.qq.com/miniprogram/dev/devtools/download.html

  2. 扫码登录

  3. 新建项目

目录结构

redirectTo 跳转页面

<button bindtap="toDetail">去详情页</button>

toDetail(){
      wx.redirectTo({
        url: '/pages/detail/detail'
      })
}

wx.showToast

<button bindtap='showToast' >弹窗</button>

  showToast(){
    wx.showToast({
      title: '前端林木',
      icon:'success',
      duration:2000
    })
  },

wx.showLoading

<button bindtap="showloading">弹窗2</button>

  showloading(){
    wx.showLoading({
      title: '加载中',
    })
    setTimeout(function () {
      wx.hideLoading()
    }, 2000)
  },

wx.showModal

<button bindtap="showModal">弹窗3</button>

  showModal(){
    wx.showModal({
      title: '提示',
      content: '这是⼀一个模态弹窗',
      success(res) {
        if (res.confirm) {
          console.log('⽤用户点击确定')
        } else if (res.cancel) {
          console.log('⽤用户点击取消')
        }
      }
    })
  },

云函数引入

云开发文档:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/basis/getting-started.html

// 除project.config.json文件外所有文件全部放到 frontend中
//project.config.json
{
  "miniprogramRoot": "mini-fe/",
  "cloudfunctionRoot": "mini-back/",
}
//backend/sum/index.js
// 云函数入口文件

const cloud = require('wx-server-sdk')
cloud.init()
// 云函数入口函数
exports.main = (event, context) => {
  console.log('cosole.log',event)
  console.log(context)
  return {
    sum: event.a + event.b
  }
}
// app.js
onLaunch: function () {
  console.log('onLaunch...')
  // 这里添加
  wx.cloud.init()
},
wx.cloud.callFunction({
  // 云函数名称
  name: 'add',
  // 传给云函数的参数
  data: {
    a: 1,
    b: 2,
  },
  success(res) {
    console.log(res.result.sum) // 3
  },
  fail: console.error
})
wx.cloud.callFunction({
  // 云函数名称
  name: 'add',
  // 传给云函数的参数
  data: {
    a: 1,
    b: 2,
  },
})
  .then(res => {
    console.log(res.result) // 3
  })
  .catch(console.error)
上一篇 下一篇

猜你喜欢

热点阅读