unicloud学习

发布时间 2023-09-12 14:04:44作者: 二口甜

阿里云  :云服务空间   云函数  云对象  云数据库

每个uniApp有一个唯一的appid,每个服务空间有一个唯一的spaceid。

服务空间和手机端项目是多对多绑定关系。同DCloud账号下,一个应用可以关联到多个服务空间。一个服务空间也可以被多个项目访问。

访问云函数:uniCloud.callFunction

uniCloud.callFunction({
name:"myCloud-function",
data:{
num:this.num
}
}).then(res=>{
console.log(res)
this.listArr = res.result;
})

连接数据库:const  db = uniCloud.database();

promise

promise是解决异步的方法,本质上是一个构造函数,可以用它实例化一个对象。对象身上有resolve、reject、all,原型上有then、catch方法。

promise对象有三种状态:pending(初识状态/进行中)、resolved或fulfilled(成功)、rejected(失败)

getNav(callback){
return new Promise((resolve,reject)=>{
uni.request({
url:"https://ku.qingnian8.com/dataApi/news/navlist.php",
success:res=>{
resolve(res)
},
fail:err=>{
reject(err)
}
})
})
}

调用:

/promise链式调用
this.getNav().then(res=>{
let id=res.data[0].id;
return this.getArticle(id);
})

 

高级用法:

await / async ES7的新规范,异步处理同步化 

async onLoad() {
let id,res;
res=await this.getNav();
id=res.data[0].id;
res=await this.getArticle(id);
id=res.data[0].id;
res=await this.getComment(id);
console.log(res)

出处:作者:咸虾米_ https://www.bilibili.com/read/cv18799030/ 出处:bilibili