LeanCloud使用基础

  • 在框架中安装leancloud。
$ npm install leancloud-storage --save
  • 在main.js中引入并初始化
import AV from "leancloud-storage"



AV.init({//初始化

  appId: "HTkEWe8***************",

  appKey: "mjEmB2***************",

  serverURL: "https://**************.com"

});

 Vue.prototype.$AV = AV //挂载在全局

初始化、表名等需要的信息在应用程序中

  • 挂载到全局后就可以使用了
  • 查询表中所有数据:(这是封装后的方法) 参数传表名
  • //查询表中的所有数据,参数为表名
        async Search_db(db_name) {
          const query = new this.$AV.Query(db_name);
          return query.find();
    
    
        // 查询最新一条数据
        //const query = new this.$AV.Query("update");
    	//			query.descending("createdAt");
    	//			let data = await query.first();
    
        },

    调用:

  • this.Search_db("MeteorologicalWarning").then((arr) => {
              
            });

    筛选:

  •  //根据字段名、值查询数据库
        Search_key_db(key, value) {
          const query = new this.$AV.Query("Subscribers");
          query.equalTo(key, value);
          return query.find();
        },

    调用与上相同

  • 其余花式查询方法在官网api中都有:
    数据存储开发指南 · JavaScript - LeanCloud 文档

  • 插入数据:

  • // 声明 class
              const Todo = this.$AV.Object.extend("MeteorologicalWarning"); //表名
              // 构建对象
              const todo = new Todo();
              // 为属性赋值,字段名必须和数据库中严格一致,传递的值的数据类型必须和数据库中的严格一致
              todo.set("Type", this.ruleForm.type);
              todo.set("Title", this.ruleForm.title);
              todo.set("Region", this.ruleForm.region);
              todo.set("Enable", this.ruleForm.Enable);
              todo.set("DefenseGuide", this.ruleForm.DefenseGuide);
              todo.set("Date1", this.ruleForm.date1);
              todo.set("Date2", this.ruleForm.date2);
              todo.set("Company", this.ruleForm.company);
              todo.set("Body", this.ruleForm.body);
              todo.set("People", this.ruleForm.People);
              // 将对象保存到云端
              todo.save().then(
                (todo) => {
                  // 成功保存之后,执行其他逻辑的回调
                  
                },
                (error) => {
                  // 异常处理
                  console.log(error);
                  this.$notify.error({
                    title: "错误",
                    message: "数据库错误!",
                  });
                }
              );

    修改数据:

  • that.$AV.Object.createWithoutData(
                    "MeteorologicalWarning",//表名
                    that.id//每一个数据对象都有一个id,获取数据的时候可以拿到
                  )
                    .set("Enable", that.change)//更新的字段名、更新的字段值
                    .save()
                    .then(function () {
                      //更新成功执行的回调
                      
                    });

    删除数据:

  • const todo = this.$AV.Object.createWithoutData(
                "MeteorologicalWarning",//表名
                that.id //数据对应的id
              );
              //删除成功
              todo.destroy().then(
                (todu) => {
                  //删除成功执行的回调
                },
                //删除失败
                (error) => {
                  //删除失败执行的回调
                }
              );

    每条数据对应的id在获取数据时可以拿到。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值