uniappt怎么获取用户列表并显示到页面上,给个例子
时间: 2024-02-20 16:01:45 浏览: 22
UniApp可以通过uni-id模块的getUserInfo()方法获取用户列表,并通过页面渲染展示。以下是一个获取用户列表并展示到页面的简单例子:
1. 在页面上定义一个数据变量users用于存储用户列表:
```
<template>
<view>
<ul>
<li v-for="(user, index) in users" :key="index">{{ user.username }}</li>
</ul>
</view>
</template>
<script>
export default {
data() {
return {
users: [],
}
},
mounted() {
this.getUsers()
},
methods: {
getUsers() {
uniCloud.callFunction({
name: 'getUserList',
}).then(res => {
this.users = res.result.data
})
},
},
}
</script>
```
2. 在云函数中定义getUserList方法用于获取用户列表:
```
'use strict';
const db = uniCloud.database()
exports.main = async (event, context) => {
const users = await db.collection('uni-id-users').get()
return users
};
```
这样,当页面加载时,会调用getUsers方法获取用户列表,并通过v-for指令将每个用户的用户名渲染到页面上。