本文為大家分享了幾點微信小程式開發技巧,希望能幫助到廣大開發者。
1、全域變數的使用
每個小程式都需要在app.js 中呼叫App 方法註冊小程式範例,綁定生命週期回呼函數、錯誤監聽和頁面不存在監聽函數等。
詳細的參數意義和使用請參考 App 參考文件 。
整個小程式只有一個 App 實例,是全部的頁面共享。開發者可以透過 getApp 方法取得到全域唯一的 App 範例,取得App上的資料或呼叫開發者註冊在 App 上的函數。
我們在做小程式的時候往往需要大量的請求,而請求的網域也都是相同的,我們可以把網域儲存到全域變數中,這樣會方便後面請求網域的修改。 (user_id、unionid、user_info之類常用到的都可以放在全域變數中)
//app.js App({ globalData: { user_id: null, unionid:null, url:"https://xxx.com/index.php/Home/Mobile/", //请求的域名 user_info:null } })
當在頁面中使用時記得要引用下app.js,小程式已經提供了方法
//index.js //获取应用实例 const app = getApp() //获取app //let url = app.globalData.url; //使用方法,可先定义或者直接使用app.globalData.url wx.request({ url: app.globalData.url + 'checkfirst', //就可以直接在这里调用 method:'POST', header:{"Content-Type":"application/x-www-form/"} data:{}, success:(res)=>{}
2.箭頭函數的使用
當我們呼叫介面請求時要透過請求傳回的資料改變頁面資料經常要用到臨時指標來保存this指標。
但如果使用ES6的箭頭函數就可以避免
使用臨時指標
onLoad: function (options) { let that = this //保存临时指针 wx.request({ url: url + 'GetCouponlist', method: 'POST', header: { 'Content-Type': 'application/x-www-form-urlencoded' }, data: { }, success(res) { that.setData({ //使用临时指针 coupon_length:res.data.data.length }) } })
使用ES6箭頭函數( ) => {}
success:(res) => { this.setData({ //此时this仍然指向onLoad coupon_length:res.data.data.length }) }
3.HTTP請求方法的封裝
在小程式中http請求是很頻繁的,但每次打出wx.request是很煩的,而且程式碼也是冗餘餘的,所以我們要把他封裝起來
首先要在utils資料夾中新建一個js,我命名為request.js,在裡面封裝出post和get的請求,記得最後要宣告出來
//封装请求 const app = getApp() let host = app.globalData.url /** * POST 请求 * model:{ * url:接口 * postData:参数 {} * doSuccess:成功的回调 * doFail:失败回调 * } */ function postRequest(model) { wx.request({ url: host + model.url, header: { "Content-Type": "application/x-www-form-urlencoded" }, method: "POST", data: model.data, success: (res) => { model.success(res.data) }, fail: (res) => { model.fail(res.data) } }) } /** * GET 请求 * model:{ * url:接口 * getData:参数 {} * doSuccess:成功的回调 * doFail:失败回调 * } */ function getRequest(model) { wx.request({ url: host + model.url, data: model.data, success: (res) => { model.success(res.data) }, fail: (res) => { model.fail(res.data) } }) } /** * module.exports用来导出代码 * js中通过 let call = require("../util/request.js") 加载 */ module.exports = { postRequest: postRequest, getRequest: getRequest }
這一步非常重要記得新增!
module.exports = { postRequest: postRequest, getRequest: getRequest }
使用時就在對應的頁面頂部呼叫,Page外部噢
let call = require("../../utils/request.js")
#使用的時候↓
get
//获取广告图 call.getRequest({ url:'GetAd', success:(res)=>{ //箭头函数没有指针问题 this.setData({ urlItem: res.data }) } })
post
call.postRequest({ url: 'addorder', data: { shop_id: that.data.shop_id, user_id: app.globalData.user_id, coupon_sn: that.data.coupon_sn, carType: that.data.car_type, appointtime: that.data.toTime }, success:(res)=>{ console.log(res) wx.navigateTo({ url: '../selectPay/selectPay?order_sn=' + res.data.order_sn + '&fee=' + res.data.real_pay + "&order_id=" + res.data.order_id, }) } })
4.搜尋input中,如何點擊搜尋按鈕進行搜尋及按鈕樣式修改
正常我們會在搜尋框中加入一個搜尋按鈕,點擊進行搜索,但是小程序不是操作dom的,所以是無法直接取得到input中的值,所以要透過另外的方法進行搜尋。
(1)透過input元件中的bindconfirm屬性(confirm-type="search" 可將軟鍵盤的完成按鈕改為「搜尋」)
<input class='search_input' type='text' confirm-type='search' bindconfirm='toSearch' ></input> //js部分 toSearch(e){ console.log(e.detail.value) //e.detail.value 为input框输入的值 }
(2)利用form表單的提交,來完成點選按鈕的提交(input需要新增name屬性)
搜尋按鈕
利用button取代form的表單提交( form-type="submit"),注意用view不行,必須用button
需要自己修改button的預設樣式(button的邊框要在button::after中修改)
//wxml部分 <form bindsubmit="formSubmit" bindreset="formReset"> <input class='search_input' type='text' confirm-type='search' name="search" bindconfirm='toSearch' > <button class='search_btn' form-type='submit'>搜索</button></input> </form>
//js部分 formSubmit(e){ console.log(e.detail.value.search) //为输入框的值,input记得添加name属性 }
相關推薦:小程式開發教學
以上是總結幾點小程式開發技巧的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

禪工作室 13.0.1
強大的PHP整合開發環境