当前位置:网站首页>微信小程序云开发|个人博客小程序
微信小程序云开发|个人博客小程序
2022-08-01 20:25:00 【橙子!】
1.前言
这篇文章详细的介绍了个人博客小程序
的云开发流程,包括博客展示页面,添加博客页面的创建,以及云函数的上传,数据库的创建和使用。同时使用到了form,text等组件以及使用富文本
添加博客。
本程序所有数据都存储在云开发里面,不需要开发者自己的服务器。功能包括:云数据库,云函数,云存储等,是一个小程序项目学习的保姆级教程!欢迎大家学习。
2.博客首页数据展示
首先,我们设计博客展示的静态页面
。每篇博客包含头像,昵称,简介,内容,图片等数据组成,我们将其设计为一个方块展示,并且每个方块使用flex布局样式。整体布局分为两个结构,上下结构:上面显示用户信息下面显示博客。左右结构:显示用户头像昵称等。
- 修改小程序的标题:
"navigationBarTitleText": "个人博客小程序",
- 设置博客页面结构:
<!--博客展示-->
<view class="blog-block">
<view class="blog-card">
<view class="blog-user">
<image class="avatar" src="../../images/头像 女孩.png"></image>
<view class="username">橙子</view>
<button open-type="share" style="width: 60rpx;">
<image class="icon-share" src="../../images/上传.png"></image>
</button>
</view>
<view class="blog-item">
<view class="blog-user">这里显示博客的简介</view>
<view class="image-block">
<image src="../../images/1.png" mode="widthFix"></image>
</view>
</view>
</view>
</view>
- 设置博客页面样式:
/**部分样式代码展示*/
.blog-block{
margin-bottom: 30rpx;
padding: 20rpx;
}
.blog-card {
margin-top: 30rpx;
padding-bottom: 30rpx;
width: 100%;
display: flex;
flex-direction: column;
align-items: center;
background-color: #fff;
border-radius: 6rpx;
}
.blog-item {
width: 95%;
}
- 效果展示:
3.使用data中的数据渲染博客展示
上面我们已经做好了静态页面,接下来通过index.js
中的data
的数据来渲染页面的显示,首先来定义blogs
的数据结构,包含了以下的字段来实现数据动态绑定:
blogs:[{
avatar:"../../images/头像 女孩.png",
name:"橙子",
brief:"这里显示博客的简介",
content:"博客的内容",
img:"../../images/1.png",
time:10000
}
]
在wxml
文件中修改代码实现数据动态绑定,同时可以使用for循环来渲染多篇博客的显示,此时的item
指的是blogs
里面的每一个对象。方法如下:
<image src="{
{item.img}}" mode="widthFix"></image>
这样就实现了数据的动态绑定,但是数据是写死的,只能在程序中维护数据,所以我们就要创建云数据库,把数据记录放在程序后端服务器。
4.使用云数据库创建集合blogs
创建云数据库的目的是为了将博客的数据放到云开发的云数据库中,方便维护!云数据库在云开发控制台中创建:
这里创建了一个blogs
集合用于存储数据,系统会自动生成一个id作为主键。这时可以在这里添加数据记录。
5.读取数据库中的数据
创建好云数据库以后我们就要实现数据的读取,这里分为三个步骤:1.和数据库建立链接。2. 找到读取数据的集合。 3.读取所需数据。
初始化数据库:
const db =wx.cloud.database()
读取数据并将data赋值给blogs:
db.collection("blogs").get({
success:res=>{
this.setData({
blogs:res.data
})
}
})
这样我们就实现了将云数据库的数据渲染到前端页面,但是这样我们还是没有提供前端添加博客的功能,于是,下一步我们要添加一个页面,用于用户添加博客!
6.创建添加博客页面
下一步我们将添加一个新的页面用于用户添加博客,并且设置tabBar。创建方法:右击Pages选择新建文件夹,命名为blogAdd
,右击新创建的文件夹选择新建文件,命名为blogAdd
,注意两者名字要相同!
下一步添加tabBar,在app.json
文件中tabBar对象的list属性中添加:
{
"pagePath": "pages/blogAdd/blogAdd",
"iconPath": "images/user.png",
"selectedIconPath": "images/user-active.png",
"text": "添加博客"
}
其中iconPath
中添加tabBar未选中时的图标,selectedIconPath
中添加选中时的图标。
添加了新的页面以后,为了方便调试,此时可以添加编译模式,修改启动页面,下次编译就直接跳转到修改的页面而不是首页。
7.博客添加页面样式设计
接下来要完成的是该页面的布局样式和功能实现。主要使用到了form组件,input组件,textarea组件以及rich-text组件等。
先来做博客添加的静态页面,给页面添加label组件,input组件,button组件,textarea组件等:
<!--头像(后面设置为自动获取微信头像)-->
<view class="form-group">
<label class="form-label">头像</label>
<input value="../../images/头像 女孩.png" name="avatar" class="form-control" placeholder="请输入头像" />
<button type="primary" bindtap="onGetUserProfile" style="width:100rpx;padding:0rpx;margin:0rpx;">授权</button>
</view>
<!--内容(后面要使用富文本输入)-->
<view class="form-group form-column">
<label class="form-label">内容</label>
<editor class="form-editor"></editor>
</view>
<!--按钮提交-->
<view style="margin-top:30rpx;margin-bottom:60rpx;">
<button type="primary" form-type="submit">保存</button>
</view>
该页面部分样式展示:
.form-group {
padding: 20rpx 10rpx;
display: flex;
flex-direction: row;
align-items: center;
border-bottom: 1rpx solid #dfdfdf;
}
.form-label {
color: #191919;
width: 150rpx;
padding-left: 15rpx;
height: 60rpx;
line-height: 60rpx;
letter-spacing: 1rpx;
}
.form-title{
font-size: 32rpx;
color: #7f7f7f;
width: 100%;
}
效果展示:
8.总结
现在已经实现了博客展示页面,博客添加页面的样式布局,主要用到一些微信小程序云开发的基础知识,下一篇文章我们要实现form表单提交数据,小程序中富文本编辑器的使用,本地图片的预览和选择以及云函数的一些相关操作。
边栏推荐
- [Multi-task learning] Modeling Task Relationships in Multi-task Learning with Multi-gate Mixture-of-Experts KDD18
- 外骨骼机器人(七):标准步态数据库
- KDD2022 | 自监督超图Transformer推荐系统
- 使用Huggingface在矩池云快速加载预训练模型和数据集
- 【Untitled】
- SIPp installation and use
- 线上问题排查常用命令,总结太全了,建议收藏!!
- SIPp 安装及使用
- Different operating with different locks, rounding
- "Torch" tensor multiplication: matmul, einsum
猜你喜欢
Win10, the middle mouse button cannot zoom in and out in proe/creo
KDD2022 | 自监督超图Transformer推荐系统
[Multi-task optimization] DWA, DTP, Gradnorm (CVPR 2019, ECCV 2018, ICML 2018)
30-day question brushing plan (5)
[Energy Conservation Institute] Ankerui Food and Beverage Fume Monitoring Cloud Platform Helps Fight Air Pollution
WhatsApp group sending actual combat sharing - WhatsApp Business API account
虚拟机的IP地址自动变为127.0.0.1
LabVIEW 使用VISA Close真的关闭COM口了吗
【kali-信息收集】(1.5)系统指纹识别:Nmap、p0f
Arthas 常用命令
随机推荐
我的驾照考试笔记(4)
myid file is missing
线上问题排查常用命令,总结太全了,建议收藏!!
Fork/Join线程池
【节能学院】智能操控装置在高压开关柜的应用
终于有人把AB实验讲明白了
The configuration manual for the secondary development of the XE training system of the missing moment document system
用户身份标识与账号体系实践
[Energy Conservation Institute] Application of Intelligent Control Device in High Voltage Switchgear
MongoDB快速上手
【Dart】dart构造函数学习记录(含dart单例模式写法)
58:第五章:开发admin管理服务:11:开发【管理员人脸登录,接口】;(未实测)(使用了阿里AI人脸识别)(演示了,使用RestTemplate实现接口调用接口;)
多线程之生产者与消费者
Where should I prepare for the PMP exam in September?
我的驾照考试笔记(2)
Interview Blitz 70: What are sticky packs and half packs?How to deal with it?
Different operating with different locks, rounding
Which websites are commonly used for patent searches?
Remove 360's detection and modification of the default browser
Redis 做网页UV统计