当前位置:网站首页>uniCloud address book combat

uniCloud address book combat

2022-08-02 04:32:00 GentleTK

1 新建uni-app项目——通讯录

HBuilder 选择文件,新建(Install a plugin if prompted,按照提示安装即可,Login will be requested later,如果没有HBuilder账户,You can follow the prompts to register):

image-20210723150057300

2 Create cloud service spaces and associate projects

uniCloud 目录上单击右键,选择打开 uniCloud Web 控制台,Follow the prompts to create a cloud service space:

image-20210723150241790

uniCloud 目录上单击右键,选择 关联云服务空间或项目,Select the cloud service space you just created:

image-20210723150453854

3 创建数据表

在WebClick on Cloud Database in the console创建数据表

image-20210723150749770

Click Table Structure,编辑 read 属性值为 true

image-20210723150946596

4 录入数据

点击 添加记录

image-20210723151345693

List of data after adding records:

image-20210723151613854

5 Front-end creates components,读取数据表

pages 目录上单击右键,点击新建页面,新建 list 页面:

image-20210723151857950

点击预览,运行当前项目,然后修改 list.vue 如下:

<template>
	<view>
		<unicloud-db v-slot:default="{data, loading, error, options}" collection="contacts">
			<view v-if="error">{
   {error.message}}</view>
			<view v-else>
				{
   {data}}
			</view>
		</unicloud-db>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				
			}
		},
		methods: {
			
		}
	}
</script>

<style>

</style>

运行出现如下错误:

QQ图片20210723153401

database 目录上单击右键,点击下载所有DB schema及扩展校验函数,After refreshing, you can see the queried data:

image-20210723153725630

DCloud 插件市场中搜索 uni-ui ,点击使用HBuilderX 导入插件

image-20210723154745197

image-20210723154926104

修改 list.vue 如下,实现通讯录列表:

<template>
	<view>
		<unicloud-db v-slot:default="{data, loading, error, options}" collection="contacts">
			<view v-if="error">{
   {error.message}}</view>
			<view v-else>
				<uni-list>
					<uni-list-item v-for="item in data" :key="item._id" :title="item.name" :note="item.phone" link></uni-list-item>
				</uni-list>
			</view>
		</unicloud-db>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				
			}
		},
		methods: {
			
		}
	}
</script>

<style>

</style>

点击预览,出现scss/sassCompile plugin not installed error,Follow the prompts to install:

image-20210723155633815

修改 list.vue 如下,Implement long press to delete records:

<template>
	<view>
		<unicloud-db ref="udb" v-slot:default="{data, loading, error, options}" collection="contacts">
			<view v-if="error">{
   {error.message}}</view>
			<view v-else>
				<uni-list>
					<uni-list-item v-for="item in data" @longpress.native="rmItem(item._id)" :key="item._id" :title="item.name" :note="item.phone" link></uni-list-item>
				</uni-list>
			</view>
		</unicloud-db>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				
			}
		},
		methods: {
			rmItem(id) {
				this.$refs.udb.remove(id)
			}
		}
	}
</script>

<style>

</style>

The effect after long press is as follows:

image-20210723160718919

Clicking OK will prompt that the permission verification failed:

image-20210723160753134

可以修改 database/contacts.schema.json 文件的 delete 字段值为 true,然后在 contacts.schema.json 上单击右键,点击上传DB Schema,Then click OK to delete,删除后如下所示:

image-20210723161036613

pages 目录上单击右键,点击新建页面,新建 add 页面,修改 add.vue 如下,Implement the new record function:

<template>
	<view>
		<uni-easyinput  v-model="item.name" placeholder="姓名" />
		<uni-easyinput  v-model="item.phone" placeholder="电话" />
		<button type="default" @click="submit">提交</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				item:{
					"name":"",
					"phone":""
				}
			}
		},
		methods: {
			submit() {
				const db = uniCloud.database();
				db.collection('contacts').add(this.item).then(e=>{
					console.log(e)
				})
			}
		}
	}
</script>

<style>

</style>

运行效果如下:

image-20210723163530115

The permission check failed,只需修改 database/contacts.schema.json 文件的 create 字段值为 true,然后在 contacts.schema.json 上单击右键,点击上传DB Schema,Then click OK to add,I got an error after adding it:Database validation failed:提交的字段[“name”,“phone”]in the local datasheetschema文件中不存在".

修改 database/contacts.schema.json 文件如下所示:

{
    
	"bsonType": "object",
	"required": [],
	"permission": {
    
		"read": true,
		"create": true,
		"update": false,
		"delete": true
	},
	"properties": {
    
		"_id": {
    
			"description": "ID,系统自动生成"
		},
		"name":{
    },
		"phone":{
    }
	}
}

然后在 contacts.schema.json 上单击右键,点击上传DB Schema,After submitting again, the effect is as follows:

image-20210723164158113

回到列表页,发现已经添加成功了:

image-20210723164248820

pages 目录上单击右键,点击新建页面,新建 update 页面,修改 update.vue 如下,Implement the update record function:

<template>
	<view>
		<uni-easyinput  v-model="item.name" placeholder="姓名" />
		<uni-easyinput  v-model="item.phone" placeholder="电话" />
		<button type="default" @click="submit">更新</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				item:{
					"_id":"",
					"name":"",
					"phone":""
				}
			}
		},
		onLoad({item}) {
			this.item = JSON.parse(item)
		},
		methods: {
			submit() {
				const db = uniCloud.database();
				let item = {...this.item}
				delete item._id
				db.collection('contacts').doc(this.item._id).update(item).then(e=>{
					console.log(e)
				})
			}
		}
	}
</script>

<style>

</style>

修改 list.vue 如下:

<template>
	<view>
		<unicloud-db ref="udb" v-slot:default="{data, loading, error, options}" collection="contacts">
			<view v-if="error">{
   {error.message}}</view>
			<view v-else>
				<uni-list>
					<uni-list-item v-for="item in data" @click.native="updateFn(item)" @longpress.native="rmItem(item._id)" :key="item._id" :title="item.name" :note="item.phone" link></uni-list-item>
				</uni-list>
			</view>
		</unicloud-db>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				
			}
		},
		methods: {
			updateFn(item){
				uni.navigateTo({
					url: '../update/update?item='+JSON.stringify(item),
					success: res => {},
					fail: () => {},
					complete: () => {}
				})
			},
			rmItem(id) {
				this.$refs.udb.remove(id)
			}
		}
	}
</script>

<style>

</style>

修改 database/contacts.schema.json 文件如下所示:

{
    
	"bsonType": "object",
	"required": [],
	"permission": {
    
		"read": true,
		"create": true,
		"update": true,
		"delete": true
	},
	"properties": {
    
		"_id": {
    
			"description": "ID,系统自动生成"
		},
		"name":{
    },
		"phone":{
    }
	}
}

然后在 contacts.schema.json 上单击右键,点击上传DB Schema,After the preview runs,在listOn the page, click a contact to enter the modification page:

image-20210723170745238

点击更新后,回到list页面刷新后:

image-20210723170827335

文章作者:GentleTK
原文链接:https://gentletk.gitee.io/uniCloud通讯录实战

原网站

版权声明
本文为[GentleTK]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/214/202208020323581001.html