總結幾點小程式開發技巧

王林
發布: 2021-02-19 09:32:13
轉載
3361 人瀏覽過

總結幾點小程式開發技巧

本文為大家分享了幾點微信小程式開發技巧,希望能幫助到廣大開發者。

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=&#39;search_input&#39; type=&#39;text&#39; confirm-type=&#39;search&#39; bindconfirm=&#39;toSearch&#39; ></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=&#39;search_input&#39; type=&#39;text&#39; confirm-type=&#39;search&#39; name="search" bindconfirm=&#39;toSearch&#39; >
 <button class=&#39;search_btn&#39; form-type=&#39;submit&#39;>搜索</button></input>
</form>
登入後複製
//js部分
formSubmit(e){
 console.log(e.detail.value.search) //为输入框的值,input记得添加name属性
}
登入後複製

相關推薦:小程式開發教學

#

以上是總結幾點小程式開發技巧的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:jb51.net
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!