uniapp 页面刷新,数据存储,数据上传,关闭当前页面打开指定页面,数据添加,云函数数据获取和添加

本文介绍了如何在Vue应用中使用uniCloud进行数据获取和存储,展示了前端表单提交、列表渲染以及云函数的调用过程,以实现在前端收集用户输入并同步至云端数据库。
摘要由CSDN通过智能技术生成
<template>
	<view class="content">

		<view class="buttompadding">
			<view class="row" v-for="item in listarr" :key="item._id">
				<view class="name">
					{{item.title}}
				</view>
			</view>
		</view>



		<view class="container">
			<view class="title">
				<input type="text" v-model="title" placeholder="请输入内容" />
			</view>
			<view class="onbutton">
				<button @click="onsubmit">提交</button>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: '',
				listarr: []
			}
		},
		onLoad() {
			//页面初始化调用数据
			this.getdata()
		},
		methods: {
			//获取数据的getdata方法

			getdata() {
				uniCloud.callFunction({
					name: "getnotebookdata",
					data: {}
				}).then(res => {

					this.listarr = res.result.data
					console.log(this.listarr);
				})

			},


			//点击事件,提交数据
			onsubmit() {
				console.log("用户点击了提交");
				//将数据加入数组中
				const newText = {
					title: this.title
				}; // 要添加的文本
				this.listarr.push(newText); // 使用 push() 方法将文本添加到数组
				console.log(this.listarr);
				//连接云函数进行数据存储
				uniCloud.callFunction({
					name: "contentupload",
					data: {
						title: this.title
					}
				}).then(res => {
					console.log(res);
				})
				//清空输入框
				this.title = ""
				//加载数据
				this.getdata()
				uni.redirectTo({
					url: '/pages/index/index', // 关闭当前页面跳转到另外一个页面
				});
			}
		}
	}
</script>

<style>
	.container {
		display: flex;
		justify-content: space-between;
		/* 上面这串代码是作用是让元素平均分布
	  下面这串代码的作用是规定元素的排列方式
	  flex-direction: column-reverse; */
		position: fixed;
		right: 30rpx;
		left: 30rpx;
		bottom: 15rpx;
		padding: 30rpx 0 0 0;
	}

	.name {
		padding: 15rpx 30rpx;
	}

	.buttompadding {
		padding: 0 0 150rpx;
	}
</style>

这是主要代码

const db=uniCloud.database()
exports.main = async (event, context) => {
	let arr= await db.collection("notebook").orderBy("_id","desc").get();
	return arr
};


这是云函数,用于获取数据

const db=uniCloud.database();
exports.main = async (event, context) => {
	let {title}=event
	let res=await db.collection("notebook").add({
		title
	})
	return res;
};

这个云函数是用于添加数据

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星辰漫步者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值