uni-app云开发

一、新建

 二、云服务器空间

先创建在进行关联 

 三、云函数

创建云函数

 上传云函数

 页面调用

			uniCloud.callFunction({
						name: "base",
						data: {
							name: 'wt',
							age: 18
						}
					})
					.then(res => {
						uni.showModal({
							content: JSON.stringify(res.result)
						})
					})

云函数编写

四、云数据库 

1、创建

2、新增数据 

3、表结构

 4、下载表结构

 5、运行项目

6、展示数据 

<template>
	<view class="content">
		<button @click="call" class="title">呼叫服务器</button>
		<unicloud-db ref="udb" v-slot:default="{data,loading,error,options}" collection="concat">
			<view v-if="error">
				{{error.message}}
			</view>
			<view v-else>
				<uni-list>
					<uni-list-item link :to="'/pages/update/update?item='+JSON.stringify(item)"
						@longpress.native="$refs.udb.remove(item._id)" v-for="item in data" :key="item._id"
						:title="item.username" :note="item.tel">
					</uni-list-item>
				</uni-list>
			</view>
		</unicloud-db>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: 'Hello'
			}
		},
		onLoad() {

		},
		onShow() {
			if (this.$refs && this.$refs.udb)
				this.$refs.udb.refresh()
		},
		methods: {
			call() {
				uniCloud.callFunction({
						name: "base",
						data: {
							name: 'wt',
							age: 18
						}
					})
					.then(res => {
						uni.showModal({
							content: JSON.stringify(res.result)
						})
					})
					.catch(err => {
						console.log(err);
					})
			},
			// 清理全部数据
			// let res = await collection.get()
			// res.data.map(async (document) => {
			// 	return await collection.doc(document.id).remove();
			// });

		}
	}
</script>

<style>

</style>

<template>
	<view>
		<uni-easyinput v-model="item.username" placeholder="用户名"></uni-easyinput>
		<uni-easyinput v-model="item.tel" placeholder="电话"></uni-easyinput>
		<button class="title" @click="addConcat">添加</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				item: {
					username: "",
					tel: ''
				}
			}
		},
		methods: {
			addConcat() {
				//数据库
				var db = uniCloud.database();
				//获取表
				db.collection("concat")
					//执行添加
					.add(this.item)
					//成功
					.then(res => {
						uni.showToast({
							title: "添加成功"
						})
					})
			}
		}
	}
</script>

<style>

</style>

<view v-else>
				<uni-list>
					<uni-list-item link :to="'/pages/update/update?item='+JSON.stringify(item)"
						@longpress.native="$refs.udb.remove(item._id)" v-for="item in data" :key="item._id"
						:title="item.username" :note="item.tel">
					</uni-list-item>
				</uni-list>
			</view>

<template>
	<view>
		<uni-easyinput v-model="item.username" placeholder="用户名"></uni-easyinput>
		<uni-easyinput v-model="item.tel" placeholder="电话"></uni-easyinput>
		<button class="title" @click="updateConcat">更新</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				item: {
					username: "",
					tel: ''
				}
			}
		},
		onLoad(option) {
			this.item = JSON.parse(option.item)
		},
		methods: {
			updateConcat() {
				//获取item
				var item = {
					...this.item
				};
				//删除id
				delete item._id;
				const db = unCloud.database();
				//获取数据库
				db.collection("concat")
					.doc(this.item._id) //查询一条
					.update(item)
					.then(res => {
						uni.showToast({
							title: "更新成功"
						})
						uni.navigateBack();
					})
					.catch(err => {
						uni.showModel({
							title: JSON.stringify(err)
						})
					})
			}
		}
	}
</script>

<style>

</style>

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值