赞
踩
<template> <view class="content"> <view class="buttompadding"> <view class="row" v-for="item in note" :key="item.index"> <view class="name"> {{item.title}} </view> </view> </view> <view class="inputstyle"> <input class="title" type="text" v-model="title" placeholder="请输入内容" /> <button class="buttonstyle" @click="onpush">添加</button> </view> <view class="z"> </view> </view> </template> <script> export default { data() { return { note: [], listarr: [], title: '', copytitle: '' } }, onLoad() { // 在页面中保存文本到本地 // uni.setStorageSync('note', '2023年1月要看一本书'); // 在页面中从本地读取文本并显示在屏幕上 // this.note = uni.getStorageSync('note'); // console.log(this.note); // 输出 '2023年1月要看一本书' //页面初始化调用数据 this.getdata() this.onsubmit() }, methods: { //获取数据的getdata方法 getdata() { uniCloud.callFunction({ name: "getnotebookdata", data: {} }).then(res => { this.listarr = res.result.data // console.log(this.listarr); // //将数据进行重新排列,按照正序 this.listarr.sort((a, b) => { return a._id.localeCompare(b._id); }); // 数据存储到本地 uni.setStorageSync('note', this.listarr); }) }, onsubmit() { this.note = uni.getStorageSync('note'); console.log(this.note); // 本地数据的读取 }, onpush() { //将数据加入数组中 const newText = { title: this.title }; // 要添加的文本 this.note.push(newText); // 使用 push() 方法将文本添加到本地数组中 console.log(this.note); //打印本地数据 this.copytitle = this.title this.title = '' //连接云函数进行数据存储 uniCloud.callFunction({ name: "contentupload", data: { title: this.copytitle } }).then(res => { console.log(res); }) } } } </script> <style> .content { padding: 30rpx 30rpx; } .inputstyle { /* position的优先级要比title的高一些,需要优先设置position */ position: fixed; right: 30rpx; left: 30rpx; bottom: 15px; background-color: #FFFFFF; display: flex; justify-content: space-between; .title { flex: 1; } } .name { padding: 15rpx 0 0; } .content { padding: 15rpx 30rpx 170rpx; } .z{ background-color: #FFFFFF; position: fixed; right: 30rpx; left: 30rpx; bottom: 0; height: 15px; } </style>
这是主要代码
const db=uniCloud.database();
exports.main = async (event, context) => {
let {title}=event
let res=await db.collection("notebook").add({
title
})
return res;
};
这是用于添加数据的云函数
const db=uniCloud.database()
exports.main = async (event, context) => {
let arr= await db.collection("notebook").orderBy("_id","desc").field({'title':true}).limit(15).get();
return arr
};
这是用于读取数据的云函数
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。