寫在前面的話:算不了入門教程,只能算這幾晚的摸索教程,下次會出一篇一文入門小程序
javascript
本文示例源碼:https://github.com/lotapp/BaseCode/tree/master/javascript/5.wechat/basephp
在線瀏覽:https://github.lesschina.com/js/5.wechat/1.小程序摸索.htmlcss
有些同志留言說我消失了,文章更新頻率比之前慢多了?我這邊先統一回復一下:html
最近這幾個月
利用空閒時間把三國讀完了(咳,別問我爲何讀,就是忽然想讀了)前端
PS:有空聊聊唄~java
而後最近兩個星期
迷上了讀書,回頭可能會發點讀書筆記,推薦幾本不錯的書給你們git
而後就是上次發文
,修復Shopee上傳限制的時候使用了下JQ
來快速實現github
PS:說到底仍是個後端偏數據方向的,也就
JQ
還記得。。。數據庫
而後本着專研的精神,又把H5
複習了一下小程序
PS:幾年前曾經在舊博客寫過H5的讀書筆記
原本準備找下思惟導圖快速過下的,以後發現。。。當時懶了一下,只是發圖,因此。。丟包了
。。。
PS:因而最近幾天閒暇之餘
W3C
逛的比較多
而後先是改寫了上次的腳本,再寫了H5
的markdown草稿
PS:過幾天大家應該能夠看到了,咳,人老了~容易疲勞。。讓我先緩緩。。先緩緩
而後專研的毛病又來了,草稿寫到導航和多媒體的時候想用小程序試試,畢竟如今是小程序的天下了
PS:以前前給親戚弄小程序的時候研究了1個晚上,發現不是很難,而後如今想到了
而後發現~前端的東西的確不難,可是我這人有個毛病:喜歡追求頁面的美感。。
因而發現。。demo幾分鐘搞定了,爲了調個漂亮樣式卻耗去幾小時。。。
PS:原本今天能夠早點休息的,因而乎~陪你們熬個夜吧。。。
我用的很少,簡單引入一下,小程序API用起來很簡單的,更全的能夠去官方查看
PS:唉,仍是Python官方文檔最省心啊~
話說,之後仍是老老實實先把上面幾個系列文章清了再說吧~
PS:清了之後,我還會回來的~
小程序|公衆號
登陸註冊頁面:https://mp.weixin.qq.com/
這個太簡單,就不浪費時間了,貼個官方教程:https://developers.weixin.qq.com/miniprogram/dev/#申請賬號
下載開發工具:https://developers.weixin.qq.com/miniprogram/dev/devtools/download.html
新建個簡單項目:
項目基本配置:通常在詳細裏面改就好了
文件簡單介紹:(圖說的很清楚了)
PS:用法和css同樣,在小程序中像素單位所有用rpx
(相似於rem
)
rpx
: 能夠根據屏幕寬度進行自適應,文檔:https://developers.weixin.qq.com/miniprogram/dev/framework/view/wxss.html
PS:小程序支持的標籤:https://developers.weixin.qq.com/miniprogram/dev/component/
下面開始就記錄下遇到的小技術點:
源碼:https://github.com/lotapp/BaseCode/tree/master/javascript/5.wechat/base/pages/1data
在頁面的data中定義了幾個字段:
想在頁面中顯示只須要寫成{{xxx}}
便可
PS:你每次保存,左邊都會有預覽的
{{xxx}}
也能夠在樣式和屬性中哦~(wxss和css用法同樣
)
貼下demo:(支持的標籤看官方文檔便可)
<!--index.wxml--> <view class='container'> <view class='{{my_class}}'>{{name}}</view> <view>{{age}}</view> <view>{{work.name}}</view> <view>{{work.location}}</view> </view>
腳本:(支持ES6語法)
//index.js Page({ data: { name: "小明", age: 23, work: { "name": "微軟", "location": "中國" }, my_class: "red" }, onLoad: function() { console.log("頁面加載完成"); } })
樣式:
/* index.wxss */ view { padding: 10rpx; } .red { color: red; }
效果演示:
知識點
:
this.data.xxx
this.setData({xxx:xx})
bindtap="自定義方法"
屬性來個修改頁面初始數據的案例:(官方說先修改js中的值,而後異步修改頁面中的值)
<!--index.wxml--> <view class='container'> <view class='{{my_class}}'>{{name}}</view> <view>{{age}}</view> <view>{{work.name}}</view> <view>{{work.location}}</view> <button bindtap='update_info'>點我就修改</button> </view>
腳本文件:
//index.js Page({ data: { name: "小明", age: 23, work: { "name": "微軟", "location": "中國" }, my_class: "red" }, onLoad: function() { console.log("頁面加載完成"); }, // 自定義方法 // look:新增內容 update_info: function() { // this對象常常容易變,我通常都存一份 var that = this; // 後臺獲取data裏的值 console.log(that.data.name, that.data.age) // 修改data(直接賦值沒用) that.setData({ age: 25, name: "小華", work: { "name": "蘋果", "location": "美國" } }); } })
效果展現:
官方文檔:https://developers.weixin.qq.com/miniprogram/dev/api/wx.showToast.html
源碼:https://github.com/lotapp/BaseCode/tree/master/javascript/5.wechat/base/pages/2show
知識點:wx.showToast({title:'內容',icon:'什麼圖標',duration:多少毫秒})
在View中添加按鈕:
<button bindtap='show_msg1'>點我彈兩行</button> <button bindtap='show_msg2'>點我就成功</button>
腳本中添加自定義方法:
Page({ data: { title1: '你知道嗎?這是能夠顯示多行的彈框提醒~\r\n你知道嗎?這是能夠顯示多行的彈框提醒~\r\n你知道嗎?這是能夠顯示多行的彈框提醒~', title2: '一二三四五六七八' }, onLoad: function() { console.log("頁面加載完成"); }, // 彈框提醒 show_msg1: function() { wx.showToast({ title: this.data.title1, icon: 'none', // 能夠顯示2行 duration: 2000 // 默認1500 }) }, // 彈框提醒 show_msg2: function() { // 默認只能顯示7箇中文字 wx.showToast({ title: this.data.title2 }) } })
效果演示:(\r\n
可能在PC調試的時候不換行,可是能夠在手機中換行)
源碼:https://github.com/lotapp/BaseCode/tree/master/javascript/5.wechat/base/pages/3data
說這個以前,先普及一下H5的這方面知識:
自定義屬性
:在標籤中的data-自定義屬性名
(爲了規範化)
dom.dataset.自定義屬性名
or dom.dataset["自定義屬性名"]
dom.dataset.自定義屬性名 = xxx
or dom.dataset["自定義屬性名"] = xxx
delete dom.dataset.自定義屬性名
or delete dom.dataset["自定義屬性名"]
dom.getAttribute("屬性名")
dom.removeAttribute("屬性名")
dom.setAttribute("屬性名", "值")
dom.hasAttribute("屬性名")
舉個栗子:
<div class="test" data-name="mmd" data-test-one="test">自定義屬性</div> <script> // 獲取標籤的自定義屬性值 let list = document.querySelector(".test").dataset; // 獲取:dom.dataset.自定義屬性名(屬性名不包含`data-`) console.log(list.name); // PS:test-one名字會改爲駝峯命名的變量:testOne console.log(list.testOne) // 設置:dom.dataset.自定義屬性名 = xxx or dataset[自定義屬性名] = xxx list.name = "小明"; // 標籤中對應值會變成小明 list.age = 23; // 添加一個屬性 // PS:設置爲data-test-two list.testTwo = "test2"; </script>
輸出效果:
官方文檔:https://developers.weixin.qq.com/miniprogram/dev/framework/view/wxml/event.html
知識點:設置hover樣式
:在標籤內置了hover-class="xxx"
屬性
view:
<view class='container'> <view data-name='小張' data-age='22' bindtap='get_datas' hover-class='hover'>點我獲取data值</view> </view>
js:(ES6語法忘記的同志能夠去以前寫的一文讀懂ES6)
Page({ data: { title: '獲取Data屬性的值' }, onLoad: function(options) { // 設置標題 wx.setNavigationBarTitle({ title: this.data.title, }); }, get_datas: function(e) { console.log(e); let infos = e.currentTarget.dataset; // 顯示彈框 wx.showToast({ title: `Name:${infos.name},Age:${infos.age}`, // ES6語法 icon: 'none' }) } })
樣式:
.hover { color: green; }
動態演示:
官方文檔:https://developers.weixin.qq.com/miniprogram/dev/api/wx.setNavigationBarTitle.html
源碼:https://github.com/lotapp/BaseCode/tree/master/javascript/5.wechat/base/pages/4title
知識點:wx.setNavigationBarTitle({title:"xxx"})
view:
<view class='container'> <text>這是一個測試頁面</text> </view>
js文件:
Page({ data: { title: '歡迎光臨' }, onLoad: function(options) { // 設置標題 wx.setNavigationBarTitle({ title: this.data.title, }); } })
效果演示:
官方文檔:https://developers.weixin.qq.com/miniprogram/dev/api/wx.makePhoneCall.html
源碼:https://github.com/lotapp/BaseCode/tree/master/javascript/5.wechat/base/pages/6tel
知識點:wx.makePhoneCall({phoneNumber:"xx"})
view:
<view class='container'> <view hover-class='hover'> <text data-tel='{{tel}}' bindtap='call_tel'>{{info}}{{tel}}</text> </view> </view>
js:
Page({ data: { info: "客服電話:", tel: "95017" }, onLoad: function(options) {}, call_tel: function() { // 打電話 wx.makePhoneCall({ phoneNumber: this.data.tel }); } })
動態演示:
源碼:https://github.com/lotapp/BaseCode/tree/master/javascript/5.wechat/base/pages/5img
這個和html同樣,CSS3就能夠實現了
wxml:
<view class='container bg'> <view>This is Test</view> </view>
wxss:
.container { height: 1500rpx; } .bg { /* 設置背景圖片的尺寸 */ background-size: 100% 100%; /* CSS3 */ /* 不支持本地圖片,可使用Base64或者容許域名下的圖片 */ background-image: url(data:image/jpeg;base64,/9j/4AAQSkZJ...省略); }
動態展現:
官方文檔:https://developers.weixin.qq.com/miniprogram/dev/component/image.html
PS:圖片懶加載:lazy-load="true"
wxml:
<view class='container'> <!-- 高度自適應 --> <image class="bg" src="../../images/bg.jpg" mode="widthFix"></image> </view>
wxss:
.container { padding: 0rpx; } .bg { width: 100%; }
動態展現:
其實不少時候都是由於height設置100%,它不生效,因此才各類樣式來調節
能夠這樣設置來達到目的:設置寬度100%,高度100vh
(整個屏幕默認滿屏爲100vh)
PS:地圖用的比較多
擴展:
源碼:https://github.com/lotapp/BaseCode/tree/master/javascript/5.wechat/base/pages/7goto
知識點
:
wx.navigateTo({url:"url地址"})
<navigator url='url地址'>xxx</navigator>
open-type
屬性能夠關注一下:先看一下目錄結構:
index頁面
<view class='container'> <view> <navigator url='{{url}}' hover-class='hover'>連接跳轉</navigator> </view> <view hover-class='hover' bindtap='to_page' data-url='{{url}}'>方法跳轉</view> <view hover-class='hover' bindtap='goto_page' data-url='{{url2}}'>帶參跳轉</view> </view>
index腳本:
Page({ data: { name: '小明', age: 22, url: '../1data/update_info', url2: './main' }, onLoad: function(options) { }, to_page: e => { // 頁面跳轉 wx.navigateTo({ url: e.currentTarget.dataset.url }) }, goto_page: function(e) { var that = this; // 頁面跳轉 wx.navigateTo({ url: `${e.currentTarget.dataset.url}?name=${that.data.name}&age=${that.data.age}` }); } })
main頁面:
<view class='container'> <view>{{name}}</view> <view>{{age}}</view> </view>
main腳本:
Page({ data: {}, onLoad: function(pms) { console.log(pms); var that = this; // 設置data值 this.setData({ name: pms.name, age: pms.age }); } })
動態演示:
這些東西都是用API,算是比較簡單的了,就是找起來麻煩點,時間不早了,地圖這塊我再貼一個案例就先結束吧
源碼:https://github.com/lotapp/BaseCode/tree/master/javascript/5.wechat/base/pages/8map
知識點
:
wx.getLocation({success:成功執行的方法,fail:失敗執行的方法})
:獲取經緯度wx.openLocation({latitude: 經度值, longitude: 緯度值})
:顯示對應位置的地圖wxml:
<view> <button bindtap='get_location'>{{demo1}}</button> <button bindtap='open_location'>{{demo2}}</button> </view>
js源碼:
Page({ data: { demo1: '獲取經緯', demo2: '打開地圖', lon: 120.636146, lat: 31.25893 }, onLoad: function(options) {}, // 須要使用this的時候,最外面方法老老實實寫function() get_location: function() { var that = this; // 獲取經緯度 wx.getLocation({ // 成功的時候 success: res => { console.log(res.latitude, res.longitude, res.speed, res.accuracy); // 更新頁面數據 that.setData({ lon: res.longitude, lat: res.latitude }); // 彈框提醒 wx.showToast({ title: `(${res.longitude},${res.latitude})`, // ES6語法 icon: 'none' }); }, // 失敗的時候 fail: ex => { // 彈框提醒 wx.showToast({ title: '定位未受權,請從新受權:\r\n刪除小程序後再打開', icon: 'none' }); } }); }, // 打開位置 open_location: function() { var that = this; // 打開位置 wx.openLocation({ latitude: that.data.lat, longitude: that.data.lon }); } })
簡單說下成功以後的參數含義:
res.longitude
:經度res.latitude
:緯度res.speed
:移動速度(實時定位的時候用的多)res.accuracy
:精確度(通常低於50,經緯數據就誤差太多)失敗摸擬:
成功摸擬:
額外說明:須要配置一下你須要使用的權限
PS:受權以後,在開發的時候能夠清楚狀態(現實中從新受權須要刪除小程序再打開
)
官方文檔:https://developers.weixin.qq.com/miniprogram/dev/component/map.html
先看下簡單案例的擴展:
在打開地圖的時候指定address
能夠更人性化顯示:
Page({ data: { lon: 120.674297, lat: 31.324571 }, open_location: function() { var that = this; // 打開位置 wx.openLocation({ latitude: that.data.lat, longitude: that.data.lon, // scale: 10, // 縮放級別(5~18)默認是18 address: '江蘇省蘇州市工業園區都市花園' // 這個信息能夠經過地圖api逆向解析 }); } })
效果:
知識點
:
show-location
:顯示當前定位點markers
:多個標識bindmarkertap
:標記點擊事件width:100%;height:100vh
wxml:
<view> <map longitude='{{lon}}' latitude='{{lat}}' markers='{{markers}}' show-location='true' bindmarkertap='makertap' style='width:100%;height:100vh'></map> <!-- --> </view>
JS:
Page({ data: { lon: 120.674297, lat: 31.324571, markers: [] }, // 頁面加載 onLoad: function() { var that = this; // eg:能夠經過baidu Map獲取到markers信息 // BMap.regeocoding({success: ret => {ret.wxMarkerData}}); // 假設經過API獲取到了數據 that.setData({ markers: [{ id: 0, latitude: that.data.lat, longitude: that.data.lon, address: '江蘇省蘇州市工業園區都市花園', iconPath: '/images/marker_red.png', callout: { 'content': '江蘇省蘇州市工業園區都市花園', 'bgColor': '#fff', 'color': '#f00', 'padding': 15, 'display': 'ALWAYS', // BYCLICK:點擊顯示 'borderRadius': 5 } }] }); }, // 標記點擊事件 makertap: function(e) { var that = this; // 提示 wx.showToast({ title: `點擊了標記點${e.markerId}`, icon: 'none' }); // 能夠根據e.markerId獲取marker信息 console.log(that.data.markers[e.markerId]); } })
動態演示:
PS:能夠經過經緯信息來獲取對應的位置信息(最後一個百度地圖的demo裏有貼
)
官方文檔:http://lbsyun.baidu.com/index.php?title=wxjsapi/guide/getlocation
我還寫了一個慣連的案例,這邊就不演示了,給你們課後自測吧:
PS:在使用外部連接的時候須要添加到白名單中(補充說明裏有貼哦~)
開發的時候能夠在手機中預覽,也能夠真機調試把輸出信息直接顯示到PC端:
手機調試能夠打開調試模式:
以後的操做都會記錄,並且控制檯輸出也會顯示:
有了AppID,並不能夠開發,還須要是開發者|管理員:
在沒有上線前,職工開發和體驗成員使用:
在使用外部連接的時候須要添加到白名單中(網站必須備案過)
先要上傳代碼:
這時候能夠選擇
PS:提交覈審後就能夠上線了(不推薦把本身作的demo提交覈審,騰訊會處罰的~)
如今小程序提供了雲開發
的API(能夠當作數據庫
+文件上傳下載
)感興趣的能夠先了解下:
https://developers.weixin.qq.com/miniprogram/dev/wxcloud/basis/getting-started.html
時候不早了,建議你們明天再看這篇文章(待續...)