小程序 --- 項目小練手Ⅰ

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.pswp.cn/news/250234.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/250234.shtml
英文地址,請注明出處:http://en.pswp.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下&…