p3-体验uniCloud云函数

137 阅读1分钟

云函数包含若干个子概念,包含云对象,云函数,公共模块等。

  • 云对象是普通云函数的升级
  • 新建云对象

云对象的url划,定时触发,依赖哪些模块都放在package.json中

  • 云对象
module.exports = {
	say() {
		return {
			errCode: 0,
			data: "hello, I'm uniCloud"
		}
	}
}
  • 调用云对象,客户端接收服务端的参数案例
<template>
	<view class="content">
			<button @click="callco">呼叫服务器</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: ''
			}
		},
		methods: {
			// 云对象的方法是异步的,需要注意
			async callco() {
				const co1 = uniCloud.importObject('co1')
				let res = await co1.say()
				uni.showModal({
					content: JSON.stringify(res),
					showCancel: false
				})
			}
		}
	}
</script>

案例一.png

案例一.png

客户端向服务端传递参数

  • 云对象
module.exports = {
	say(text) {
		console.log(text)
		return {
			errCode: 0,
			data: text
		}
	}
}
  • 客户端
<template>
	<view class="content">
			<button @click="callco">呼叫服务器</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: ''
			}
		},
		methods: {
			// 云对象的方法是异步的,需要注意
			async callco() {
				const co1 = uniCloud.importObject('co1')
				let res = await co1.say('yyyyy')
				uni.showModal({
					content: JSON.stringify(res),
					showCancel: false
				})
			}
		}
	}
</script>
  • 案例二.png

案例二.png