美文网首页
小程序入门学习10--云开发03

小程序入门学习10--云开发03

作者: 阿墨呦 | 来源:发表于2020-02-19 15:20 被阅读0次

npm 安装 vant-weapp
添加编译模式

在这里插入图片描述

1 添加数据
addToDo.wxml

<form bindsubmit="onSubmit">

<input name="title"></input>
<button form-type="submit">提交</button>
</form>

addToDo.js

//初始化数据库
const db = wx.cloud.database();
//创建实例
const todos = db.collection('todos');
Page({
  onSubmit:function(event){
    console.log(event.detail.value.title)
    //新增数据
    todos.add({
      data:{
        title:event.detail.value.title
      }
    }).then(res=>{
      console.log(res)
      //展示toast
      wx.showToast({
        title: 'Success',
        icon: 'success'
      })
    })
  }
})

2 查看所有数据
index.wxml

<van-cell-group>
<!-- 列表项 -->
<block wx:for="{{tasks}}">
  <van-cell title="{{item.title}}"/>
</block>

 </van-cell-group>

index.js

const db = wx.cloud.database();
const todos = db.collection('todos');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //若task为null则无concat方法,报错:Cannot read property 'concat' of null
    tasks:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData();    
  },

  onReady: function () {

  },
  onShow: function () {

  },
  //触底事件
  onReachBottom:function(){
    this.getData();
  },
  //下拉刷新事件对应"enablePullDownRefresh":true
  onPullDownRefresh:function(){
    this.getData(res=>{
      //保证数据更新完时 停止刷新
      wx.stopPullDownRefresh();
      //下拉刷新后回到第一条数据
      this.pageData.skip = 0;
    });
  },
  //获取数据
  getData:function(callback){
    //callback不存在,自动赋值
    if(!callback){
      callback=res=>{}
    }
    wx.showLoading({
      title: '数据加载中',
    })
    todos.skip(this.pageData.skip).get().then(res => {
      let oldData = this.data.tasks;
      this.setData({
        //前面数据与跳转后获得的数据进行拼接
        tasks: oldData.concat(res.data)
      },res=>{
        //跳转到第20条数据后
        this.pageData.skip=this.pageData.skip+20
        wx.hideLoading()
        //执行匿名函数
        callback();
      })
    })
  },
  //跳页
  pageData:{
    skip:0
  }
})

index.json

{
  "enablePullDownRefresh":true

}

3 查看对应数据详情
wxml

<van-cell-group>
<!-- border=false去除下划线 -->
   <van-cell  title="{{task.title}}" border="{{false}}" value="{{task.status==='in-progress'?'进行中':'已完成'}}"/>
</van-cell-group>

js

const db = wx.cloud.database()
const todos = db.collection('todos')
Page({

  data: {
    task:{}
  },
  //存放数据
  pageData:{},
  onLoad: function (options) {
    this.pageData.id = options.id
    //获取数据
    todos.doc(options.id).get().then(res=>{
      this.setData({
        //将结果赋值给data
        task:res.data
      })
    })
   
    
  },
  onReady: function () {

  }
})

4 添加图片及改动页面
addToDo.wxml

<form bindsubmit="onSubmit">
  <input name="title"></input>
  <block wx:if="{{image}}">
    <image src="{{image}}"></image>
  </block>
  <button bindtap="selectImage">选择图片</button>
  <button form-type="submit">提交</button>
</form>

addToDo.js

//初始化数据库
const db = wx.cloud.database();
//创建实例
const todos = db.collection('todos');
Page({
  data:{
    image:null
  },
  //上传图片
  selectImage:function(e){
    wx.chooseImage({
      //不使用function(res..),避免this被覆盖
      success: res=> {
        console.log(res.tempFilePaths[0])
        wx.cloud.uploadFile({
          //云端路径 保证每次名字不同 `为反引号
          cloudPath:`${Math.floor(Math.random()*10000000)}.png`,
          //上传文件路径
          filePath: res.tempFilePaths[0]
        }).then(res=>{
          console.log(res.fileID)
          //数据保存到image
          this.setData({
            image:res.fileID
          })
        }).catch(err=>{
          console.error(err)
        })
      },
    })
  },
  onSubmit:function(event){
    console.log(event.detail.value.title)
    //新增数据
    todos.add({
      data:{
        title:event.detail.value.title,
        image:this.data.image
      }
    }).then(res=>{
      console.log(res._id)
      //展示toast
      wx.showToast({
        title: '添加成功',
        icon: 'success',
        //成功后跳转到详情页
        success:res2=>{
          wx.redirectTo({
            //反引号
            url:`../todoInfo/todoInfo?id=${res._id}`,
          })
        }
      })
    })
  }
})

todoInfo.wxml

<van-cell-group>
<!-- border=false去除下划线 -->
   <van-cell  title="{{task.title}}" border="{{false}}" value="{{task.status==='in-progress'?'进行中':'已完成'}}"/>
</van-cell-group>
<image wx:if="{{task.image}}" src="{{task.image}}"></image>

index.wxml

<!-- 跳转到添加页面 -->
<view class='plusBtn'>
  <navigator url="../addToDo/addToDo">
    <van-icon class=".plusIcon" name="plus" />
  </navigator>
</view>
<van-cell-group>
  <!-- 列表项 -->
  <block wx:for="{{tasks}}">
    <!-- 点击跳转到todoInfo -->
    <navigator url='../todoInfo/todoInfo?id={{item._id}}'>
      <van-cell title="{{item.title}}" />
    </navigator>
  </block>

</van-cell-group>

index.wxss

.plusBtn{
  position: fixed;
  z-index: 100;
  bottom:150rpx;
  right:375rpx;
  background-color:#ff0;
  border-radius: 100rpx;
  width:80rpx;
  height:80rpx;
  line-height:100rpx; 
}
.plusIcon{
  font-size:80rpx;
}

app.json

{
  "pages": [
    "pages/index/index",
    "pages/addToDo/addToDo",
    "pages/todoInfo/todoInfo"
    
  ],
  "window": {
    "backgroundColor": "#F6F6F6",
    "backgroundTextStyle": "light",
    "navigationBarBackgroundColor": "#F6F6F6",
    "navigationBarTitleText": "云待办",
    "navigationBarTextStyle": "black"
  },
    "usingComponents": {
      "van-button": "vant-weapp/button",
      "van-cell": "vant-weapp/cell/index",
      "van-cell-group": "vant-weapp/cell-group/index",
      "van-icon": "vant-weapp/icon/index"
    }
}
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
领取限量云产品优惠

相关文章

  • 小程序入门学习10--云开发03

    npm 安装 vant-weapp添加编译模式 1 添加数据addToDo.wxml addToDo.js 2 查...

  • 小程序云开发教程(初识篇)

    目录一、为什么学小程序云开发二、小程序云开发是什么三、学习准备 系列教程小程序云开发教程(入门篇)小程序云开发教程...

  • 小程序云开发教程(地雷篇,持续更新)

    目录一、代码坑二、IDE缺陷 系列教程小程序云开发教程(初识篇)小程序云开发教程(入门篇) 一、代码坑 二、IDE...

  • 微信小程序知识全景思维导图

    微信小程序思维导图 腾讯云小程序学习地址 小程序开发文档 小程序开发社区

  • 微信小程序

    微信小程序入门 入门简易教程 小程序开发流程注册小程序->代码开发->提审->上线/审核未通过 开发者工具左侧模拟...

  • 小程序云开发入门

    1、在微信开发工具中新建小程序云开发模板,点击确定 2、创建test环境并部署环境,相当于mysql的数据库名字,...

  • 小程序云开发-入门

    创建小程序的时候,选择不使用云开发,不然的话会自动生成好多文件。自己创建配置就可以了 首先配置云函数存储的目录 操...

  • 小程序云开发入门

    最近用小程序的云开发功能写了个小程序,发现云开发这个东西真的是前端开发者的福音,因为你几乎不需要懂任何后端的东西就...

  • 小程序云开发入门

    1、开通云开发服务 点击微信开发者工具的“云开发”图标,在弹出框里点击“开通”,同意协议后,会弹出创建环境的对话框...

  • 小程序Cloud开发归纳

    小程序云开发 小程序·云开发是微信团队联合腾讯云推出的专业的小程序开发服务。开发者可以使用云开发快速开发小程序、小...

网友评论

      本文标题:小程序入门学习10--云开发03

      本文链接:https://www.haomeiwen.com/subject/bnyafhtx.html