「这是我参与11月更文挑战的第1天,活动详情查看:2021最后一次更文挑战」。
产品概述
云开发简单来说就是可以快速构建完整的小程序、公众号、Web 应用、Flutter 客户端应用,我们只需要专注于业务逻辑的实现,开发门槛更低,效率更高,这也是官网的介绍,在实现了一个书签网站后,对常用的api调用进行总结
1、 腾讯云官网 console.cloud.tencent.com/ 首先开通云服务所需要的环境
2 、环境开通后得在本地安装Cloudbase CLI
npm install -g @cloudbase/cli
3 、安装成功后在项目中安装tcb-js-sdk
npm install tcb-js-sdk
yarn add @cloudbase/js-sdk 或者使用 npm install @cloudbase/js-sdk
4、 在写代码之前得在登录授权中将匿名登录开启
5 、在Vue3.0中连接数据库
import cloudbase from '@cloudbase/js-sdk'
export const app = cloudbase.init({
env: '你的环境ID'
})
export const auth = app.auth({ persistence: 'local' })
<script>
import { defineComponent } from 'vue'
import { auth, app } from '@/api/clouldbase'
export default defineComponent({
setup () {
auth.anonymousAuthProvider().signIn().then(() => {
console.log('登录成功')
// 在登录成功的回调中连接数据库
const db = app.database()
db.collection('nav_content').get().then(res => {
console.log(res)
})
}).catch(err => {
console.log(err)
})
}
})
</script>
6、在云开发中随数据库进行'增删改查':
数据新增
db.collection('test').add({
name: 'boyyang'
}).then(res => {
console.log(res)
})
数据查找
var db = app.database()
db.collection('test').doc('该条数据的id').get().then(res => {
// res.data 包含该记录的数据
console.log(res.data)
})
条件查询
var db = app.database()
db.collection('test').where({
name: 'boyyang'
}).get().then(res => {
console.log(res.data)
})
更新数据
var db = app.database()
db.collection('test').doc('该条数据id').update({
name: "测试"
}).then(res => {
console.log(res.data)
})
删除数据
var db = app.database()
db.collection('test').doc('doc-id').remove().then(res => {
console.log(res)
})
7、使用云存储功能 --上传图片
app.uploadFile({
// 云存储的路径
cloudPath: `images/${files[0].name}`,
// 需要上传的文件,File 类型
filePath: files[0]
}).then(res => {
data.form.cover = res.download_url
})
使用云开发大大增加了前端