手把手教你從零開發到上線一個答題微信小程序項目實戰教程之02.開發題目分類頁

項目搭建

參考上一節內容git

開發試題分類頁面

pages目錄下新建home目錄,並添加4個文件,如圖所示:
json

其中:
home.js數組

// pages/home/home.js
Page({
  data: {

  },
  onLoad: function (options) {

  },
  toTestPage: function(e){
    let testId = e.currentTarget.dataset['testid'];
    wx.navigateTo({
      url: '../test/test?testId='+testId
    })
  }
})

home.wxmlapp

<!--pages/home/home.wxml-->
<view class="page">
  <view class="page-title">請選擇試題:</view>
  <view class="flex-box">
    <view class="flex-item"><view class="item bc_green" bindtap="toTestPage" data-testId="18">IT知識</view></view>
    <view class="flex-item"><view class="item bc_red" bindtap="toTestPage" data-testId="15">遊戲知識</view></view>
    <view class="flex-item"><view class="item bc_yellow" bindtap="toTestPage" data-testId="21">體育知識</view></view>
    <view class="flex-item"><view class="item bc_blue" bindtap="toTestPage" data-testId="27">動物知識</view></view>
    <view class="flex-item item-last"><view class="item bc_green" bindtap="toTestPage" data-testId="0">綜合知識</view></view>
  </view>
</view>

home.jsonxss

{
  "navigationBarTitleText": "試題分類",
  "usingComponents": {}
}

home.wxss函數

/* pages/home/home.wxss */
.page-title {
  padding-top: 20rpx;
  padding-left: 40rpx;
  font-size: 16px;
}
.flex-box {
  display: flex;
  align-items:center;
  flex-wrap: wrap;
  justify-content: space-between;
  padding: 20rpx;
  box-sizing:border-box;
}
.flex-item {
  width: 50%;
  height: 200rpx;
  padding: 20rpx;
  box-sizing:border-box;
}
.item {
  width:100%;
  height:100%;
  border-radius:8rpx;
  display: flex;
  align-items:center;
  justify-content: center;
  color: #fff;
}
.item-last {
  flex: 1;
}

修改app.json,注意:pages/home/home必定要放到pages數組的最前,以成爲首頁。flex

{
  "pages": [
    "pages/home/home",
    "pages/index/index",
    "pages/logs/logs",
  ],
  "window": {
    "backgroundTextStyle": "light",
    "navigationBarBackgroundColor": "#fff",
    "navigationBarTitleText": "WeChat",
    "navigationBarTextStyle": "black"
  },
  "style": "v2",
  "sitemapLocation": "sitemap.json"
}

修改app.wxss,定義全局樣式ui

/**app.wxss**/
.container {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  padding: 200rpx 0;
  box-sizing: border-box;
} 

.bc_green{
    background-color: #09BB07;
}
.bc_red{
    background-color: #F76260;
}
.bc_blue{
    background-color: #10AEFF;
}
.bc_yellow{
    background-color: #FFBE00;
}
.bc_gray{
    background-color: #C9C9C9;
}

運行結果

添加試題頁

pages目錄下新建test目錄,並添加4個文件,如圖所示:
this

修改test.jsurl

// pages/test/test.js
Page({

  /**
   * 頁面的初始數據
   */
  data: {
    test_id:0
  },

  /**
   * 生命週期函數--監聽頁面加載
   */
  onLoad: function (options) {
    this.setData({test_id:options.testId})
  },

修改test.wxml

<!--pages/test/test.wxml-->
<text>我是{{test_id}}</text>

運行結果

在試題分類頁點擊某一分類,跳轉到試題頁,試題頁顯示分類id

項目源碼

項目源碼

相關文章
相關標籤/搜索