搜尋
首頁微信小程式小程式開發微信小程式之template模板介紹

微信小程式之template模板介紹

May 15, 2018 am 10:19 AM
template介紹小程式

微信小程序 template模板详解及实例

首先看一些官方的一些介绍。

模板:模板功能是通过对template 标签的属性 name=”” 去创建不同模板,通过is=”name的值”来使用。

通过上面两张图,大概能看出,使用模板可以为大量类似的布局带来便利。下面看一下我自己的一个Demo.

先放出效果图(数据来自聚合数据)

可以看到,除了选项个数的差别之外,其他布局是相同的。

下面的每一道题的模板。

<template name="carItem">
 <view class="timu">
  <view class="title">第pw_item.id题</view>
  <view class=&#39;question&#39;>pw_item.question</view>
  <view class="img" wx:if="pw_item.url!=&#39;&#39;"><image src="pw_item.url" /></view>
  <view class=&#39;select&#39;>A:pw_item.item1</view>
  <view class=&#39;select&#39;>B:pw_item.item2</view>
  <view class=&#39;select&#39; wx:if="pw_item.item3!=&#39;&#39;">C:pw_item.item3</view>
  <view class=&#39;select&#39; wx:if="pw_item.item4!=&#39;&#39;">D:pw_item.item4</view>
  <view class=&#39;content&#39;>答案:pw_item.answer</view>
  <view class=&#39;content&#39;>解释:pw_item.explains</view>
 </view>
</template>

在我们上面的代码中,除了使用template标签定义模板外,还是用了条件渲染。例如当题目为判断题的时候。CD选项是没有数据的,所以就不能显示出来,我们可以通过if语句判断是否为空来决定显示与否。

下面放出代码。

CarUtils.js

/**
 * 网络请求
 */
function request(url, subject, model, testType, success, fail) {
  if (typeof success != &#39;function&#39; || typeof fail != &#39;function&#39;) {
    return
  }
  wx.request({
    url: url,
    data: {
      key: "5f0c9315c43385f5baaa3f49b79caa8f",
      subject: subject,
      model: model,
      testType: testType,


    },
    success: function (res) {
      if (res.data.error_code == 0) {
        console.log("获取数据成功"),
          success(res.data)
      } else {
        wx.showModal({
          title: &#39;提示&#39;,
          content: &#39;res.data.reason&#39;+&#39;请重新选择&#39;,
          success: function (res) {
            if (res.confirm) {
              console.log(&#39;用户点击确定&#39;)
            }
          }
        })
        console.log("失败原因" + res.data.reason)
        fail(res.data.reason)
      }
    },
    fail: function () {
      fail(&#39;网络出现问题&#39;)
    },
  })
}
function getanswer(url,success,fail){
   if( typeof success != &#39;function&#39; || typeof fail != &#39;function&#39; ) {
  return
 }
   wx.request({
    url:url,
    data: {
      key:"0794b823b484d6e1b4186d150834ae1b",
    },
    success: function(res){
     if( res.data.error_code == 0 ) {
        console.log("获取数据成功"),
        success( res.data )
      } else {
        console.log("失败原因"+res.data.reason)
        fail( res.data.reason )
      }
    },
    fail: function() {
      fail( &#39;网络出现问题&#39; )
    },
   })
}
module.exports = {
  request: request,
  getanswer:getanswer
}

template.wxml

<template name="carItem">
 <view class="timu">
  <view class="title">第pw_item.id题</view>
  <view class=&#39;question&#39;>pw_item.question</view>
  <view class="img" wx:if="pw_item.url!=&#39;&#39;"><image src="pw_item.url" /></view>
  <view class=&#39;select&#39;>A:pw_item.item1</view>
  <view class=&#39;select&#39;>B:pw_item.item2</view>
  <view class=&#39;select&#39; wx:if="pw_item.item3!=&#39;&#39;">C:pw_item.item3</view>
  <view class=&#39;select&#39; wx:if="pw_item.item4!=&#39;&#39;">D:pw_item.item4</view>
  <view class=&#39;content&#39;>答案:pw_item.answer</view>
  <view class=&#39;content&#39;>解释:pw_item.explains</view>
 </view>
</template>

选择界面 drivercar.js

Page({
 data:{
  subject: [
   {name: &#39;1&#39;, value: &#39;科目一&#39;,checked: &#39;true&#39;},
   {name: &#39;4&#39;, value: &#39;科目四&#39;},


  ],
  model: [
   {name: &#39;c1&#39;, value: &#39;c1&#39;,checked: &#39;true&#39;},
   {name: &#39;c2&#39;, value: &#39;c2&#39;},
   {name: &#39;a1&#39;, value: &#39;a1&#39;},
   {name: &#39;a2&#39;, value: &#39;a2&#39;},
   {name: &#39;b1&#39;, value: &#39;b1&#39;},
   {name: &#39;b2&#39;, value: &#39;b2&#39;},


  ],
  testType: [
   {name: &#39;rand&#39;, value: &#39;随机(100条)&#39;,checked: &#39;true&#39;},
   {name: &#39;order&#39;, value: &#39;全部(全部)&#39;},
  ],




 },
 onLoad:function(options){
 var that = this;
  that.setData({
   subject1:"1",
   model1:"c1",
   testType1:"rand"
  })
 },
 confirm(){
  var that=this;
   wx.navigateTo({
   url: &#39;detail/detail?subject=&#39;+that.data.subject1+&#39;&model=&#39;+that.data.model1+&#39;&testType=&#39;+that.data.testType1,
  });
 },
  confirm1(){
  var that=this;
   wx.navigateTo({
   url: &#39;detail_1/detail_1?subject=&#39;+that.data.subject1+&#39;&model=&#39;+that.data.model1+&#39;&testType=&#39;+that.data.testType1,
  });
 },
 //科目类型
 subjectChange(e){ 
  var that = this;
  console.log(&#39;科目类型:&#39;+e.detail.value);
  that.setData({
   subject1:e.detail.value,


  })
 } ,
  //驾照类型
  modelChange(e){
  var that = this;
  console.log(&#39;驾照类型:&#39;+e.detail.value);
  that.setData({
   model1:e.detail.value,
  })
 } ,
 //测试类型
  testTypeChange(e){
   var that = this;
  console.log(&#39;测试类型:&#39;+e.detail.value);
  that.setData({
   testType1:e.detail.value,
  })
 } ,


})

选择界面drivercar.wxml

<view class="container">
<!--radio-->
 <view class="radio">
 <text>请选择考试类型:</text>
  <radio-group class="radio-group" bindchange="subjectChange">
    <label class="radio" wx:for="pw_subject" wx:key="subject">
      <radio value="pw_item.name" checked="pw_item.checked"/>pw_item.value
   </label>
  </radio-group>
 </view>
 <view class="radio">
 <text>请选择驾照类型:</text>
  <radio-group class="radio-group" bindchange="modelChange" >
     <label class="radio" wx:for="pw_model" wx:key="model">
        <radio value="pw_item.name" checked="pw_item.checked"/>pw_item.value
     </label>
   </radio-group>
 </view>
 <view class="radio">
 <text>请选择模式:</text>
  <radio-group class="radio-group" bindchange="testTypeChange" >
     <label class="radio" wx:for="pw_testType" wx:key="testType">
        <radio value="pw_item.name" checked="pw_item.checked"/>pw_item.value
     </label>
   </radio-group>
 </view>
  <!--button-->
  <text class="nav" bindtap="confirm">确定选择</text>
</view>

选择界面drivercar.wxss

.radio{ margin: 20rpx;}
.radio text{margin: 20rpx;}
.nav {
   border: 1px solid #DFDFDF;
  border-radius: 10px;
  text-align: center;
  width: 50%;
  float: left;
  height: 60rpx;
  line-height: 60rpx;
  margin-bottom:30rpx;
  margin-top: 30rpx;
  margin-left:25%;
  margin-right:25%;


}

题目界面detail.js

var util = require(&#39;../../../../Utils/CarUtils.js&#39;)
var url = &#39;http://api2.juheapi.com/jztk/query&#39;
var answerurl = "http://api2.juheapi.com/jztk/answers"
Page({
  data: {
    loadingHide: false,
    ResList: {
      "error_code": 0,
      "reason": "success",
      "result": {
        1: "A",
        2: "B",
        3: "C",
        4: "D",
        7: "AB",
        8: "AC",
        9: "AD",
        10: "BC",
        11: "BD",
        12: "CD",
        13: "ABC",
        14: "ABD",
        15: "ACD",
        16: "BCD",
        17: "ABCD"
      }
    },
  },
  onLoad: function (options) {


    var that = this
    var z=1;
    var mTimuLIs={}
    util.request(url, options.subject, options.model, options.testType, function (dataJson) {
      console.log(options.model + "model");
      console.log(options.testType + "testType");
      console.log(options.subject + "subject");
      console.log("请求成功00");


      mTimuLIs=dataJson["result"];
      console.log(mTimuLIs.length);
      for (var i = 0; i < mTimuLIs.length; i++) {


        //console.log(that.data.ResList.result[1]);
        var y= parseInt(mTimuLIs

题目界面 detail.wxml

<import src="../../../../common/templet.wxml"/>
<scroll-view scroll-y="true" class="page-body" >
   <template is="carItem" data="pw_item" wx:for="pw_mTimuLIs" wx:key="TimuList"/>
</scroll-view>
<loading hidden="pw_loadingHide">
  加载中...
</loading>

全局样式 app.wxss

.container {
 height:100%;
 flex: 1;
 display: flex;
 flex-direction: column;
 box-sizing: border-box;
 background-size: 100%;
} 
.item-view{
  padding: 10px;
  display: flex;
  flex-direction: column;
  border-top: 1px solid #DEDEDE;
  border-left: 1px solid #DEDEDE;
  box-shadow: 2px 2px 2px #C7C7C7;
  margin: 10px;
  border-radius: 5px;
}


.item-view .content{color: black;}
.item-view .date{ color: grey;margin-top: 10px;}
.item-view image{width: 100%;height: 400rpx;margin-top: 10rpx;}
.loading-view{display: flex;flex-direction: row; justify-content: center;align-items: center;padding: 10px;}
.timu{border: 1px solid #DFDFDF;margin: 20rpx;border-radius: 10px;}
.timu .title{font-size: 40rpx; }
.timu .question{text-indent: 20rpx;margin-left: 10rpx; padding: 10rpx;}
.timu .img{width: 100%;display:flex;flex-direction: column;align-items: center;margin: 0 auto;padding-top: 10rpx;padding-bottom: 10rpx;}
.timu .content{font-size: 30rpx;padding: 10rpx;margin-left: 20rpx }
.timu .select{font-size: 30rpx;margin-left: 30rpx;margin-right: 30rpx; padding: 20rpx; }

以上是微信小程式之template模板介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

Safe Exam Browser

Safe Exam Browser

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

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。