今天和朋友聊天說到小程序,然后看在看書,然后我們就弄了個小讀書的demo,然后現(xiàn)在分享一下。
一、先來上圖:
二、然后下面是詳細的說明
首先先說下邊的tabBar,項目采用json格式的數(shù)據(jù)配置,不得不說,現(xiàn)在這個是趨勢,.net core的配置也是這種方式了(暴露我是.net 陣營了)。
在這里好多同學會發(fā)現(xiàn)好多顏色的配置都不管用,是的,現(xiàn)在有效的顏色是有限制的,具體的大家可以進入官方文檔去查看。需要幾個tabBar,就在list里面寫幾個,本篇問是三個,所以,你看了三個。上面的iconPath那就是tabBar的圖標了,這個大小也是有限制的,40kb。然后,pagePath呢,就是此tabBar對應的頁面鏈接。text就是限制內容,這里不多說了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
"tabBar" : { "color" : "#dddddd" , "selectedColor" : "#d92121" , "borderStyle" : "white" , "backgroundColor" : "#fff" , "list" : [{ "pagePath" : "pages/index" , "iconPath" : "images/main.png" , "selectedIconPath" : "images/main-s.png" , "text" : "主頁" },{ "pagePath" : "pages/layout/hot" , "iconPath" : "images/hot.png" , "selectedIconPath" : "images/hot-s.png" , "text" : "最熱" },{ "pagePath" : "pages/layout/new" , "iconPath" : "images/new.png" , "selectedIconPath" : "images/new-s.png" , "text" : "最新" }] }, |
打開項目代碼目錄,如下:
這里發(fā)現(xiàn)樣式和wxml以及js文件全是同名的,這是默認寫法,這樣默認三個文件就關聯(lián)了。這又叫做:默認大于配置。
我們打開首頁index頁面
可以看到上面的頁面生命周期,我們可以在事件中寫我們自己要處理的事件。
其中getApp();方法獲取全局實例。
我們打開視圖頁面
這里看到箭頭指向的 wx:for=“”,這個是一個出來數(shù)組或列表對象的循環(huán)方法,而item是默認(又是默認)的單個列表元素。用不不想用item也可以起別名。
navigator就是導航標簽了,這里,類似于html中的<a>標簽,就不在說了。點擊navigator的內容頁面跳轉對應頁面,同樣是用url傳遞數(shù)據(jù)。
我們可以看到后臺的代碼:
數(shù)據(jù)可以通過url傳遞,目標頁面通過onLoad方法中的參數(shù)( 對象)獲取。這里還可以看到書的詳情是通過全局getApp獲取全局實例,獲取數(shù)據(jù)。這個數(shù)據(jù)就是在全局app.js里面,如下圖:
具體代碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
//app.js App( { getBanner: function (){ var bannerUrl=[ "../images/banner.jpg" ]; return bannerUrl; }, getOneBook: function (id){ var abook; var books = [ { id: "1" , bookUrl: "../images/img1.jpg" , bookName: "西方哲學史" , bookInfor: "關于哲學" }, { id: "2" , bookUrl: "../images/tmd.jpg" , bookName: "塔木德" , bookInfor: "關于信仰" }, { id: "3" , bookUrl: "../images/holy.jpg" , bookName: "圣經(jīng)" , bookInfor: "關于信仰" }, { id: "4" , bookUrl: "../images/yuz.jpg" , bookName: "果殼中的宇宙" , bookInfor: "關于科學" }, { id: "5" , bookUrl: "../images/dream.jpg" , bookName: "理想國" , bookInfor: "關于哲學" }, { id: "6" , bookUrl: "../images/out.jpg" , bookName: "失控" , bookInfor: "關于經(jīng)濟" } ]; for (i=0;i<books.length;i++){ if (books[i].id == id){ abook = books[i]; } } return abook; }, getBoookList: function (){ var indexList = [ { id: "1" , bookUrl: "../images/img1.jpg" , bookName: "西方哲學史" , bookInfor: "關于哲學" }, { id: "2" , bookUrl: "../images/tmd.jpg" , bookName: "塔木德" , bookInfor: "關于信仰" }, { id: "3" , bookUrl: "../images/holy.jpg" , bookName: "圣經(jīng)" , bookInfor: "關于信仰" }, { id: "4" , bookUrl: "../images/yuz.jpg" , bookName: "果殼中的宇宙" , bookInfor: "關于科學" }, { id: "5" , bookUrl: "../images/dream.jpg" , bookName: "理想國" , bookInfor: "關于哲學" }, { id: "6" , bookUrl: "../images/out.jpg" , bookName: "失控" , bookInfor: "關于經(jīng)濟" } ]; return indexList; } }) |