当前位置:网站首页>简易版 微信小程序开发之for指令、上传图片及展示效果优化
简易版 微信小程序开发之for指令、上传图片及展示效果优化
2022-07-03 03:23:00 【水w】
目录
五、for指令
编写goods页面
(1)wxml
内部相当于帮我们做了一个“for item in dataList” 循环
<!--pages/goods/goods.wxml-->
<text>商品列表</text>
<view>
<view wx:for="{
{ dataList }}">{
{index}}-{
{item}}</view>
</view>
<view>
<view wx:for="{
{ UserInfo }}">{
{index}}-{
{item}}</view>
</view>
或
<!--pages/goods/goods.wxml-->
<text>商品列表</text>
<view>
<view wx:for="{
{ dataList }}" wx:for-index="idx" wx:for-item="x">{
{idx}}-{
{x}}</view>
</view>
<view>
<view wx:for="{
{ UserInfo }}" wx:for-index="idx" wx:for-item="x">{
{idx}}-{
{x}}</view>
</view>(2)js
// pages/goods/goods.js
Page({
/**
* 页面的初始数据
*/
data: {
dataList: ["输入","色佛诞日","生日歌"],
UserInfo: {
name:"alex",
age: 18,
gender: false
}
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
}
})展示效果:

六、上传图片
编写home页面:
(1)wxml
<!--pages/home/home.wxml-->
<view class="menu">
<view class="item">
<image src="/static/link-01.png"></image>
<text>精品</text>
</view>
<view class="item">
<image src="/static/link-01.png"></image>
<text>精品</text>
</view>
<view class="item">
<image src="/static/link-01.png"></image>
<text>精品</text>
</view>
<view class="item">
<image src="/static/link-01.png"></image>
<text>精品</text>
</view>
</view>
<navigator url="/pages/list/list?id=566">跳转页面</navigator>
<view bindtap="uploadImage">请上传图片:</view>
<view class="images">
<image wx:for="{
{images}}" src="{
{item}}"></image>
</view>(2)js
// pages/home/home.js
Page({
/**
* 页面的初始数据
*/
data: {
images: ["/images/link-01.png","/images/link-02.png","/images/默认头像.jpeg"]
},
uploadImage() {
wx.chooseImage({
count: 9,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success:function(res) {
console.log(res)
}
})
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
}
})(3)wxss
/* pages/home/home.wxss */
image {
width: 100rpx;
height: 100rpx;
border-radius: 50rpx;
}
.menu {
display: flex;
/* 在水平方向排列 ,规定主轴方向*/
flex-direction: row;
/* 元素在水平方方向如何展示,规定在主轴方向如何展示:center */
justify-content: space-around;
}
.menu .item {
display: flex;
flex-direction: column;
align-items: center;
}
.images {
width: 200rpx;
height: 200rpx;
padding: 5rpx;
}
展示效果:
(1)点击“请选择图片”之后,会弹出一个选择页面 ,

(2)在弹出的选择页面 ,选择图片之后,console中会打印出选择的图片信息。

展示效果优化1:
(3)此时,我们修改uploadImage()函数,为了可以使我们选择的图片上传展示到页面上,
data: {
images: ["/images/link-01.png","/images/link-02.png","/images/默认头像.jpeg"]
},
uploadImage() {
var that = this
wx.chooseImage({
count: 9,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success:function(res) {
console.log(res)
that.setData({images: res.tempFilePaths})
}
})
},(4)重新编译,点击“请选择图片”之后,在弹出的选择页面选择图片,

点击“打开”之后,页面的展示效果为,

展示效果优化2:
(3)此时,我们修改uploadImage()函数,为了可以使我们选择的图片可以上传展示到页面已有的图片之后,
data: {
images: ["/images/link-01.png","/images/link-02.png","/images/默认头像.jpeg"]
},
uploadImage() {
var that = this
wx.chooseImage({
count: 9,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success:function(res) {
console.log(res)
// 设置images,页面上的图片自动修改
// that.setData({images: res.tempFilePaths})
// 默认图片 + 选择的图片
that.setData({images: that.data.images.concat(res.tempFilePaths)})
}
})
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
},(4)重新编译,点击“请选择图片”之后,在弹出的选择页面选择图片,

点击“打开”之后,页面的展示效果为,

边栏推荐
- Ansible简介【暂未完成(半成品)】
- Anhui University | small target tracking: large-scale data sets and baselines
- [mathematical logic] predicate logic (individual word | individual domain | predicate | full name quantifier | existence quantifier | predicate formula | exercise)
- Converts a timestamp to a time in the specified format
- What happens between entering the URL and displaying the page?
- ffmpeg下载安装教程及介绍
- Bigvision code
- Yolov5 project based on QT
- 将时间戳转为指定格式的时间
- [Chongqing Guangdong education] cultural and natural heritage reference materials of China University of Geosciences (Wuhan)
猜你喜欢

docker安装及启动mysql服务

Summary of electromagnetic spectrum

Nasvit: neural architecture search of efficient visual converter with gradient conflict perception hypernetwork training

900W+ 数据,从 17s 到 300ms,如何操作
![[MySQL] the difference between left join, right join and join](/img/d4/8684cd59cd1bd77e70bd4d7c7074c3.jpg)
[MySQL] the difference between left join, right join and join

Pat class B "1104 forever" DFS optimization idea

用Three.js做一個簡單的3D場景

为什么线程崩溃不会导致 JVM 崩溃

Use three JS make a simple 3D scene
![C programming learning notes [edited by Mr. Tan Haoqiang] (Chapter III sequence programming) 05 data input and output](/img/38/9c460fc58b62609dd02e7c61207ae6.jpg)
C programming learning notes [edited by Mr. Tan Haoqiang] (Chapter III sequence programming) 05 data input and output
随机推荐
Spark on yarn resource optimization ideas notes
QT based tensorrt accelerated yolov5
Nanning water leakage detection: warmly congratulate Guangxi Zhongshui on winning the first famous brand in Guangxi
二维数组中的元素求其存储地址
Convert binary stream to byte array
Réglez la hauteur et lancez le système. Currenttimemillis catton
C programming learning notes [edited by Mr. Tan Haoqiang] (Chapter III sequence programming) 03 operators and expressions
com.fasterxml.jackson.databind.exc.InvalidFormatException问题
@Accessors注解作用指定前缀遵守驼峰命名
VS 2019 配置tensorRT生成engine
机械臂速成小指南(八):运动学建模(标准DH法)
Mongodb master profile
Use three JS make a simple 3D scene
softmax的近似之NCE详解
Change and access of median value of listening object
PAT乙级“1104 天长地久”DFS优化思路
PHP constructor with parameters - PHP constructor with a parameter
基于Qt的yolov5工程
Bid farewell to artificial mental retardation: Mengzi open source project team received RMB 100 million financing to help NLP develop
静态网页 和 动态网页的区别 & WEB1.0和WEB2.0的区别 & GET 和 POST 的区别
https://blog.csdn.net/qq_45956730/article/details/125252305?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22125252305%22%2C%22source%22%3A%22qq_45956730%22%7D&ctrtid=UdFQC