小程序 --- 项目小练手Ⅰ

1. 接口文档

2. 帮助文档

  1. 小程序开发文档

  2. mdn

  3. 阿里巴巴字体 iconfont

3. 项目搭建

3.1 新建小程序项目

填入自己的appid: wxdbf2b5e8c2f521a3

3.2 文件结构

  • 一级目录
目录名作用
styles存放公共样式
components存放组件
lib存放第三方库
utils自己的帮助库
request自己的接口帮助库
pages存放页面.
  • app.json
{"pages": ["pages/index/index"],"windows": {"backgroundTextStyle": "light","navigationBarBackgroundColor": "#fff","navigationBarTitleText": "Marron购物","navigationBarTextStyle": "black"},"sitemapLocation": "sitemap.json"
}
  • app.wxss
()
  • app.js : 快捷键 wx-app + tab

  • pages/index.wxml

<view>首页</view>
  • pages/index.wxss

  • pages/index.js: wx-page + tab

3.3 搭建项目的页面

页面名称名称
首页index
分类页面category
商品列表页面goods_list
商品详情页面goods_detail
购物车页面cart
收藏页面collect
订单页面order
搜索页面search
个人中心页面user
意见反馈页面feedback
登录页面login
授权页面auth
结算页面pay

直接修改app.json中的属性: pages

{"pages": ["pages/index/index","pages/category/index","pages/goods_list/index","pages/goods_detail/index","pages/cart/index","pages/collect/index","pages/order/index","pages/search/index","pages/user/index","pages/feedback/index","pages/login/index","pages/auth/index","pages/pay/index"],"window": {"backgroundTextStyle": "light","navigationBarBackgroundColor": "#fff","navigationBarTitleText": "Marron - 购物车","navigationBarTextStyle": "black"},"style": "v2","sitemapLocation": "sitemap.json"
}

微信的编辑器会帮助自动生成对应的页面文件

3.4 引入字体图标

  1. 打开阿里巴巴字体图标(网站)
  2. 选择的图标
  3. 添加至项目
  4. 下载到本地
  5. 将样式文件由css修改为wxss
  6. 小程序中引入

在全局样式中,引入某个样式

// app.wxss
@import "./styles/iconfont.wxss";

3.5 搭建项目tabbar

  • app.json中,输入tabBar + tab.生成首页的导航栏(位于手机底部)

3.6 全局样式

需求: 假设现在需要设置主题颜色为: #d81e06; 字体大小为14px.

在微信的样式中(.wxss),可以通过如下方式来定义全局变量.

/*  /app.wxss: 项目目录下 */
page{/* 主题颜色 */--themeColor: #d81e06;/* 字体大小 */font-size: 28rpx;
}

在字页面中使用主题颜色--themeColor

/* /pages/index/index.wxss: pages目录下 */
view{/* 使用主题颜色 */color: var(--themeColor)
}

3.7 顶部的导航栏

在根目录下的app.json文件中,有一个window属性,它控制的是顶部的样式(字体大小颜色、背景色…),下面说明几个常用的属性

{"window": {"navigationBarBackgroundColor": "d81e06",	// 背景色"navigationBarTitleText": "标题","navigationBarTextStyle": "white"	// 标题颜色}
}

4. 首页

4.1 搜索框

需求: 多个页面用到搜索的功能,因此把搜索框封装成一个组件,方便代码的复用

  1. 首先在根目录下的components文件夹中新建一个目录SearchInput

  2. SearchInput目录下新建组件SearchInput(开发工具自动生成组件的4个文件)

  3. 在首页导入搜索组件

/*首页位于pages目录下的index文件夹中, 找到其json文件(微信中json文件用于配置)/pages/index/index.json
*/
{"usingComponents": {"SearchInput": "../../components/SearchInput/SearchInput"}
}
  1. 上面在配置文件中,导入组件成功.下面在.wxml中使用导入的组件
<!-- /pages/index/index.wxml -->
<view class="pyg_index"><!-- 搜索框结构 --><SearchInput></SearchInput>
</view>

以上完成了搜索框外观的创建与使用, 下面实现点击跳转功能.

<!-- components/SearchInput/SearchInput.wxml -->
<view class="search_input"><navigator url="/pages/search/index" open-type="navigate">搜索</navigator>
</view>

以上实现了,点击搜索的适合,跳转到搜索页面.

4.2 轮播图

需求: 首页加载的时候,发送请求,获取数据。并将返回的结果显示在页面中

具体步骤: 首页的js文件,首先在data中注册数据,然后在onLoad生命周期函数中添加请求数据的事件.

// pages/index/index.js
Page({data:{swiperList:[],},onLoad: function(options){wx.request({url: "https://api-hmugo-web.itheima.net/api/public/v1/home/swiperdata",success: (result) =>{this.setData({swiperList: result.data.message})}})}
})

上面准备好了轮播图的数据,下面根据数据写样式

<view class="pyg_index"><!-- 搜索框 --><SearchInput></SearchInput><!-- 轮播图 --><view class="index_swiper"><swiper autoplay indicator-dots circular><swiper-item wx:for="{{swiperlist}}" wx:key="goods_id"><navigator><image mode="widthFix" src="{{item.image_src}}"></image></navigator></swiper-item></swiper></view>
</view>

上面成功的将轮播图展示在小程序中了:

  • wx:for: 使用到进入时,请求到的数据
  • wx:key: 绑定一个唯一值, 和vue中的key是一个意思
  • mode="widthFix": 表示宽度百分百,高度自适应.

下面,写样式

.index_swiper swiper {width: 750rpx;height: 340rpx;
}
.index_swiper swiper image {width: 100%;
}

以上完成了,最基本的轮播图流程(请求数据 -> 展示). 但是请求接口并未封装,可能会造成回调地狱,不利于代码的维护.下面封装请求数据的接口


方法封装

为了解决回调地狱的问题,下面使用ES6提供的语法对方法进行封装. 请求的代码写在了路径request/index.js

// request/index.js
export const request = (params) =>{return new Promise((resolve, reject)=>{wx.request({...params,success: (result) =>{resolve(result)},fail: (err) =>{reject(err)}})})
}

上面封装了一个promise请求方法,调用如下

// pages/index/index.js
import  { request } from "../../request/index.js"Page({data:{swiperList: [],},onLoad: function(options){request({url: "https://api-hmugo-web.itheima.net/api/public/v1/home/swiperdata"}).then(result =>{this.setData({swiperList: result.data.message})})}
})

4.3 分类导航

获取数据和设置数据同4.2,下面主要是页面的设计

<!-- pages/index/index.wxml -->
<view class="pyg_index"><!-- 楼层导航 --><view class="index_cate"><navigator wx:for="{{cateList}}" wx:key="name"><image mode="widthFix" src="{{item.image_src}}"></image></navigator></view>
</view>

样式如下:

.index_cate{display: flex;
}.index_cate navigator{padding: 25rpx;flex: 1;
}.index_cate navigator image{width: 100%;
}

4.4 楼层

楼层的接口

楼层请求数据和设置数据的操作同4.2。 下面编写楼层的页面

<view class="pyg_index"><!-- 楼层 --><view class="index_floor"><view class="floor_group" wx:for="{{floorList}}" wx:for-item="item1" wx:for-index="index1" wx:key="floor_title"><!--  标题 --><view class="floor_title"><image mode="widthFix" src="{{item1.floor_title.image_src}}"></image></view><!-- 内容 --><view class="floor_list"><navigator wx:for="{{item1.product_list}}" wx:for-index="index2" wx:key="name"><image mode="{{index2 == 0? 'widthFix' : 'scaleToFill'}}" src="{{item2.image_src}}"></image></navigator></view></view></view>
</view>

说明:

  • wx:for-item ='item1' 将数组中的当前项,命名为"item1"

  • <image mode="{{index2 == 0? 'widthFix' : 'scaleToFill'}}">: 数组中的第一项,使用widthFix模式,数组中除第一项外的项使用scaleToFill模式

上面实现了楼层的基本页面,下面实现样式

/* 楼层 */
.index_floor{}.index_floor .floor_group{}.index_floor .floor_group .floor_title{}.index_floor .floor_group .floor_title image{width: 100%;
}.index_floor .floor_group .floor_list{/* 清除浮动 */overflow: hidden;padding: 10rpx 0;
}.index_floor .floor_group .floor_list navigator{width: 33.33%;float: left;
}.index_floor .floor_group .floor_list navigator:nth-last-child(-n+4){height: 27.72711207vm;border-left: 10rpx solid #fff;
}.index_floor .floor_group .floor_list navigator:nth-child(2) {border-bottom: 10rpx solid #fff;
}.index_floor .floor_group .floor_list navigator:nth-child(3) {border-bottom: 10rpx solid #fff;
}.index_floor .floor_group .floor_list navigator image {width: 100%;height: 100%;
}

小结:

  • 给到数四个元素写样式: navigator:nth-last-chiuld(-n+4)
  • 给第2个子元素设置样式: navigator:nth-child(2)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/250234.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

vue aixos请求json

this.axios.get(/static/json/jinxiangZhe.json).then(res>{console.log(res);}).catch( error > {console.log(error,error)}) 转载于:https://www.cnblogs.com/SunShineM/p/9087734.html

小程序 --- Tab组件的封装

1. Tabs组件的封装 1.1 组件的引入 使用自定义的组件很简单,只需在使用组件的页面的配置文件.json文件中配置. // pages/goods_list/index.json {"usingComponents":{"Tabs": "../../components/Tabs/Tabs"} }然后再.wxml文件中使用即可 <…

爬虫之拉勾网职位获取

重点在于演示urllib.request.Request()请求中各项参数的 书写格式 譬如&#xff1a; url data headers...Demo演示&#xff08;POST请求&#xff09;:import urllib.requestimport urllib.parseimport json, jsonpath, csvurl "https://www.lagou.com/jobs/positionAjax.…

小程序 --- 点击放大功能、获取位置信息、文字样式省略、页面跳转(navigateTo)

1. 点击放大功能的实现 需求: 点击轮播图中的图片会实现放大预览的功能。首先有轮播图的样式如下 <!-- pages/goods_detail/index.wxml --> <!-- 轮播图 --> <view class"detail_swiper"><swiperautoplaycircularindicator-dots><swip…

Axure实现多用户注册验证

*****多用户登录验证***** 一、&#xff08;常规想法&#xff09;方法&#xff1a;工作量较大&#xff0c;做起来繁琐 1、当用户名和密码相同时怎么区分两者&#xff0c;使用冒号和括号来区分&#xff1a; eg. (admin:123456)(123456:demo)(zhang:san);由此得出前面是括号后面是…

前端插件网址

http://www.swiper.com.cn/转载于:https://www.cnblogs.com/luchuangao/p/9088057.html

python --- opencv部分学习

1. OpenCV 1.1 opencv概念 OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库可以运行在Linux、Windows、Android和Mac OS操作系统上它轻量级而且高效 – 有一系列C函数和少量 C 类构成同时提供了 Python、Ruby、MATLAB等语言的接口实现了图像处理和计算机视觉方面的很…

hive与hbase集成

环境: hadoop2.7.7 hive3.1.0 hbase2.0.2 1.jar包拷贝(之所以用这种方式,是因为这种方式最为稳妥,最开始用的软连接的方式,总是却少jar包)到hive的lib目录下删除所有hbase相关的jar rm -rf hbase-*.jar 接着从hbase的lib目录下拷贝所有的hbase相关jar cp -a hbasehome/lib/hba…

Winform(C#)输入完毕后,按Enter键触发Button事件

如在输入“用户名”和“密码”之后&#xff0c;有些人习惯按“回车键”来代替页面上的“确定”按钮&#xff0c;那么这一功能在Winform(C#)里如何实现呢&#xff1f; 触发密码文本框的KeyDown事件&#xff0c;代码如下&#xff1a; [c-sharp] view plaincopy private void txtP…

Maximum Xor Secondary(单调栈好题)

Maximum Xor Secondary CodeForces - 280B Bike loves looking for the second maximum element in the sequence. The second maximum element in the sequence of distinct numbers x1, x2, ..., xk (k > 1) is such maximum element xj, that the following inequa…

python --- udp的使用

1. python的模块导入规则 参考 1.1 系统自带模块 系统自带的模块直接import导入 import time import unittest1.2 第三方下载模块 第三方下载模块也可以直接导入 import HTMLTestRunner import requests1.3 导入模块的部分函数或类 from time import sleep,strftime fro…

杂项-公司:唯品会

ylbtech-杂项-公司&#xff1a;唯品会唯品会公司成立于2008年08月&#xff0c;2012年3月23日登陆美国纽约证券交易所上市&#xff08;股票代码&#xff1a;VIPS&#xff09;。成为华南第一家在美国纽交所上市的电子商务企业。主营B2C商城唯品会名牌折扣网站是一家致力于打造中高…

python --- 使用socket创建tcp服务

1. 网络-tcp 参考 1.1 tcp简介 介绍 TCP协议,传输控制协议(英语: Transmission Control Protocol, 缩写为TCP)是一种面向连接的、可靠的、基于字节流的传输层通信协议,由IETF的RFC 793定义. TCP通信需要经过创建连接、数据传送、终止连接三个步骤. TCP通信模型中,在通信开…

Linux基本的操作

一、为什么我们要学习Linux 相信大部分人的PC端都是用Windows系统的&#xff0c;那我们为什么要学习Linux这个操作系统呢&#xff1f;&#xff1f;&#xff1f;Windows图形化界面做得这么好&#xff0c;日常基本使用的话&#xff0c;学习成本几乎为零。 而Linux不一样&#xff…

汇编语言 实验4

实验4 实验内容1&#xff1a;综合使用 loop,[bx]&#xff0c;编写完整汇编程序&#xff0c;实现向内存 b800:07b8 开始的连续 16 个 字单元重复填充字数据 0403H&#xff1b;修改0403H为0441H&#xff0c;再次运行 步骤1&#xff1a;在记事本中编写好temp.asm文件 步骤2&#x…

python --- 线程

1. 多任务 - 线程 参考 首先考虑一个没有多任务的程序: import timedef sing():# 唱歌 5 秒钟for i in range(5):print("-----菊花台ing....-----")time.sleep(1)def dance():# 跳舞 5秒钟for i in range(5):print("-----跳舞.....-----")time.sleep(5)d…

Python 链接汇总

MNIST手写识别 转载于:https://www.cnblogs.com/bycnboy/p/9095199.html

17种常用的JS正则表达式 非负浮点数 非负正数

<input typetext idSYS_PAGE_JumpPage nameSYS_PAGE_JumpPage size3 maxlength5 οnkeyupthis.valuethis.value.replace(/[^1-9]\D*$/,"") οndragenter"return false" οnpaste"return !clipboardData.getData(text).match(/\D/)"" sty…

python --- 使用conda配置pytorch

使用Conda配置PyTorch 1. 添加channels 下载地址 $ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ $ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ $ conda config --add channels htt…

LDAP第三天 MySQL+LDAP 安装

https://www.easysoft.com/applications/openldap/back-sql-odbc.html OpenLDAP 使用 SQLServer 和 Oracle 数据库。 https://www.cnblogs.com/bigbrotherer/p/7251372.html          CentOS7安装OpenLDAPMySQLPHPLDAPadmin 1.安装和设置数据库 在CentOS7下&…