注冊

微信小程序開發(fā):簡單配置教程

2018-01-31
導(dǎo)讀:最近有空自學(xué)了下微信小程序,以下教程僅供自己日后查看用。 1.配置 app.json文件用來對(duì)微信小程序進(jìn)行全局配置,決定頁面文件的路徑、窗口表現(xiàn)、設(shè)置網(wǎng)絡(luò)超時(shí)時(shí)間、設(shè)置多 tab 等...

微信小程序開發(fā):簡單配置教程包括對(duì)小程序全局配置,包括page,window等基本的設(shè)置。

1.配置

app.json文件用來對(duì)微信小程序進(jìn)行全局配置,決定頁面文件的路徑、窗口表現(xiàn)、設(shè)置網(wǎng)絡(luò)超時(shí)時(shí)間、設(shè)置多 tab 等。

屬性 類型 必填 描述
pages String Array 設(shè)置頁面路徑
window Object 設(shè)置默認(rèn)頁面的窗口表現(xiàn)
tabBar Object 設(shè)置底部 tab 的表現(xiàn)
networkTimeout Object 設(shè)置網(wǎng)絡(luò)超時(shí)時(shí)間
debug Boolean 設(shè)置是否開啟 debug 模式

1.1 pages

接受一個(gè)數(shù)組,每一項(xiàng)都是字符串,來指定小程序由哪些頁面組成。每一項(xiàng)代表對(duì)應(yīng)頁面的【路徑+文件名】信息,數(shù)組的第一項(xiàng)代表小程序的初始頁面。小程序中新增/減少頁面,都需要對(duì) pages 數(shù)組進(jìn)行修改。

文件名不需要寫文件后綴,因?yàn)榭蚣軙?huì)自動(dòng)去尋找路徑下 .json, .js, .wxml, .wxss 四個(gè)文件進(jìn)行整合。

eg:

{
  "pages":[
    "pages/index/index",
    "pages/logs/logs"
  ]
}

1.2 window

用于設(shè)置小程序的狀態(tài)欄、導(dǎo)航條、標(biāo)題、窗口背景色。

屬性 類型 默認(rèn)值 描述
navigationBarBackgroundColor HexColor #000000 導(dǎo)航欄背景顏色,如"#000000"
navigationBarTextStyle String white 導(dǎo)航欄標(biāo)題顏色,僅支持 black/white
navigationBarTitleText String   導(dǎo)航欄標(biāo)題文字內(nèi)容
backgroundColor HexColor #ffffff 窗口的背景色
backgroundTextStyle String dark 下拉背景字體、loading 圖的樣式,僅支持 dark/light
enablePullDownRefresh Boolean false 是否開啟下拉刷新,詳見頁面相關(guān)事件處理函數(shù)。
onReachBottomDistance Number 50 頁面上拉觸底事件觸發(fā)時(shí)距頁面底部距離,單位為px

注:HexColor(十六進(jìn)制顏色值),如"#ff00ff"

eg:

{
  "window":{
    "navigationBarBackgroundColor": "#ffffff",
    "navigationBarTextStyle": "black",
    "navigationBarTitleText": "微信接口功能演示",
    "backgroundColor": "#eeeeee",
    "backgroundTextStyle": "light"
  }
}

1.3 tabBar

如果小程序是一個(gè)多 tab 應(yīng)用(客戶端窗口的底部或頂部有 tab 欄可以切換頁面),可以通過 tabBar 配置項(xiàng)指定 tab 欄的表現(xiàn),以及 tab 切換時(shí)顯示的對(duì)應(yīng)頁面。

Tip:

當(dāng)設(shè)置 position 為 top 時(shí),將不會(huì)顯示 icontabBar 中的 list 是一個(gè)數(shù)組,只能配置最少2個(gè)、最多5個(gè) tab,tab 按數(shù)組的順序排序。

屬性說明:

屬性 類型 必填 默認(rèn)值 描述
color HexColor   tab 上的文字默認(rèn)顏色
selectedColor HexColor   tab 上的文字選中時(shí)的顏色
backgroundColor HexColor   tab 的背景色
borderStyle String black tabbar上邊框的顏色, 僅支持 black/white
list Array   tab 的列表,詳見 list 屬性說明,最少2個(gè)、最多5個(gè) tab
position String bottom 可選值 bottom、top

其中 list 接受一個(gè)數(shù)組,數(shù)組中的每個(gè)項(xiàng)都是一個(gè)對(duì)象,其屬性值如下:

屬性 類型 必填 說明
pagePath String 頁面路徑,必須在 pages 中先定義
text String tab 上按鈕文字
iconPath String 圖片路徑,icon 大小限制為40kb,建議尺寸為 81px * 81px,當(dāng) postion 為 top 時(shí),此參數(shù)無效,不支持網(wǎng)絡(luò)圖片
selectedIconPath String 選中時(shí)的圖片路徑,icon 大小限制為40kb,建議尺寸為 81px * 81px ,當(dāng) postion 為 top 時(shí),此參數(shù)無效
eg: "tabBar": { "color": "#7A7E83", "selectedColor": "#3cc51f", "backgroundColor" : "#F7F7FA", "borderStyle": "white", "list": [{ "pagePath": "pages/word/word", "text": "背詞", "iconPath": "images/home.png", "selectedIconPath": "images/home-selected.png" } 1.4 networkTimeout

可以設(shè)置各種網(wǎng)絡(luò)請(qǐng)求的超時(shí)時(shí)間。

屬性說明:

屬性 類型 必填 說明
request Number wx.request的超時(shí)時(shí)間,單位毫秒,默認(rèn)為:60000
connectSocket Number wx.connectSocket的超時(shí)時(shí)間,單位毫秒,默認(rèn)為:60000
uploadFile Number wx.uploadFile的超時(shí)時(shí)間,單位毫秒,默認(rèn)為:60000
downloadFile Number wx.downloadFile的超時(shí)時(shí)間,單位毫秒,默認(rèn)為:60000

1.5 debug

可以在開發(fā)者工具中開啟 debug 模式,在開發(fā)者工具的控制臺(tái)面板,調(diào)試信息以 info 的形式給出,其信息有Page的注冊,頁面路由,數(shù)據(jù)更新,事件觸發(fā) 。 可以幫助開發(fā)者快速定位一些常見的問題。

1.6 page.json

每一個(gè)小程序頁面也可以使用.json文件來對(duì)本頁面的窗口表現(xiàn)進(jìn)行配置。 頁面的配置比app.json全局配置簡單得多,只是設(shè)置 app.json 中的 window 配置項(xiàng)的內(nèi)容,頁面中配置項(xiàng)會(huì)覆蓋 app.json 的 window 中相同的配置項(xiàng)。

頁面的.json只能設(shè)置 window 相關(guān)的配置項(xiàng),以決定本頁面的窗口表現(xiàn),所以無需寫 window 這個(gè)鍵,如:

屬性 類型 默認(rèn)值 描述
navigationBarBackgroundColor HexColor #000000 導(dǎo)航欄背景顏色,如"#000000"
navigationBarTextStyle String white 導(dǎo)航欄標(biāo)題顏色,僅支持 black/white
navigationBarTitleText String   導(dǎo)航欄標(biāo)題文字內(nèi)容
backgroundColor HexColor #ffffff 窗口的背景色
backgroundTextStyle String dark 下拉背景字體、loading 圖的樣式,僅支持 dark/light
enablePullDownRefresh Boolean false 是否開啟下拉刷新,詳見頁面相關(guān)事件處理函數(shù)。
disableScroll Boolean false 設(shè)置為 true 則頁面整體不能上下滾動(dòng);只在 page.json 中有效,無法在 app.json 中設(shè)置該項(xiàng)
onReachBottomDistance Number 50 頁面上拉觸底事件觸發(fā)時(shí)距頁面底部距離,單位為px

eg:

{
  "navigationBarBackgroundColor": "#ffffff",
  "navigationBarTextStyle": "black",
  "navigationBarTitleText": "微信接口功能演示",
  "backgroundColor": "#eeeeee",
  "backgroundTextStyle": "light"
}

下面展示下項(xiàng)目中的app.json的完整樣式:

{
  "pages":[
    "pages/word/word",
    "pages/search/search",
    "pages/settings/settings",
    "pages/search/detail/detail",
    "pages/settings/clause/clause",
    "pages/settings/help/help",
    "pages/settings/feedback/feedback",
    "pages/note/index/index",
    "pages/note/create/index",
    "pages/note/edit/index",
    "pages/intro/intro",
    "pages/intro/trick/trick",
    "pages/intro/recommend/recommend",
    "pages/intro/saying/saying"

  ],
  "tabBar": {
    "color": "#7A7E83",
    "selectedColor": "#3cc51f",
    "backgroundColor" : "#F7F7FA",
    "borderStyle": "white",
    "list": [{
      "pagePath": "pages/word/word",
      "text": "背詞",
      "iconPath": "images/home.png",
      "selectedIconPath": "images/home-selected.png"
    },
    {
      "pagePath": "pages/search/search",
      "text": "查詞",
      "iconPath": "images/search.png",
      "selectedIconPath": "images/search-selected.png"
    },
    {
      "pagePath": "pages/intro/intro",
      "text": "方法",
      "iconPath": "images/intro.png",
      "selectedIconPath": "images/intro-selected.png"
    },
    {
      "pagePath": "pages/note/index/index",
      "text": "筆記",
      "iconPath": "images/note.png",
      "selectedIconPath": "images/note-selected.png"
    },
    {
      "pagePath": "pages/settings/settings",
      "text": "設(shè)置",
      "iconPath": "images/settings.png",
      "selectedIconPath": "images/settings-selected.png"
    }]
  },
  "window":{
    "backgroundTextStyle":"light",
    "backgroundColor": "#EFEFF4",
    "navigationBarBackgroundColor": "#2C2D31",
    "navigationBarTitleText": "佑米單詞",
    "navigationBarTextStyle":"white",
    "enablePullDownRefresh": true
  }
}

HiShop小程序工具提供多類型商城/門店小程序制作,可視化編輯 1秒生成5步上線。通過拖拽、拼接模塊布局小程序商城頁面,所看即所得,只需要美工就能做出精美商城。

  更多小程序資訊,盡在:descansotropical.com/xiaocx/

重磅推薦:小程序開店目錄

第一部分:小商店是什么

第二部分:如何開通一個(gè)小商店

第三部分:如何登錄小商店

第四部分:開店任務(wù)常見問題

第五部分:小商店可以賣什么

第六部分:HiShop小程序特色功能

第七部分:小程序直播

第八部分:小程序收貨/物流

第九部分:小程序怎么結(jié)算

第十部分:小程序客服

第十一部分:電商創(chuàng)業(yè)

第十二部分:小程序游戲開發(fā)