温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

微信小程序如何使用template模板

发布时间:2021-07-02 14:48:36 阅读:184 作者:小新 栏目:web开发
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

这篇文章给大家分享的是有关微信小程序如何使用template模板的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

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

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

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

微信小程序如何使用template模板

微信小程序如何使用template模板

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

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

微信小程序如何使用template模板

微信小程序如何使用template模板

微信小程序如何使用template模板

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

下面的每一道题的模板。

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

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

下面放出代码。

CarUtils.js

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

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

template.wxml

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

选择界面 drivercar.js

Page({
 data:{
  subject: [
   {name'1', value'科目一',checked'true'},
   {name'4', value'科目四'},

  ],
  model: [
   {name'c1', value'c1',checked'true'},
   {name'c2', value'c2'},
   {name'a1', value'a1'},
   {name'a2', value'a2'},
   {name'b1', value'b1'},
   {name'b2', value'b2'},

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


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

  })
 } ,
  //驾照类型
  modelChange(e){
  var that = this;
  console.log('驾照类型:'+e.detail.value);
  that.setData({
   model1:e.detail.value,
  })
 } ,
 //测试类型
  testTypeChange(e){
   var that = this;
  console.log('测试类型:'+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="{{subject}}" wx:key="subject">
      <radio value="{{item.name}}" checked="{{item.checked}}"/>{{item.value}}
   </label>
  </radio-group>
 </view>
 <view class="radio">
 <text>请选择驾照类型:</text>
  <radio-group class="radio-group" bindchange="modelChange" >
     <label class="radio" wx:for="{{model}}" wx:key="model">
        <radio value="{{item.name}}" checked="{{item.checked}}"/>{{item.value}}
     </label>
   </radio-group>
 </view>
 <view class="radio">
 <text>请选择模式:</text>
  <radio-group class="radio-group" bindchange="testTypeChange" >
     <label class="radio" wx:for="{{testType}}" wx:key="testType">
        <radio value="{{item.name}}" checked="{{item.checked}}"/>{{item.value}}
     </label>
   </radio-group>
 </view>
  <!--button-->
  <text class="nav" bindtap="confirm">确定选择</text>
</view>

选择界面drivercar.wxss

.radiomargin20rpx;}
.radio text{margin20rpx;}
.nav {
   border1px solid #DFDFDF;
  border-radius10px;
  text-align: center;
  width50%;
  float: left;
  height60rpx;
  line-height60rpx;
  margin-bottom:30rpx;
  margin-top30rpx;
  margin-left:25%;
  margin-right:25%;

}

题目界面detail.js

var util = require('../../../../Utils/CarUtils.js')
var url = 'http://api2.juheapi.com/jztk/query'
var answerurl = "http://api2.juheapi.com/jztk/answers"
Page({
  data: {
    loadingHidefalse,
    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[i].answer);
         //console.log(y);
        mTimuLIs[i].answer = that.data.ResList.result[y];
        mTimuLIs[i].id=parseInt(i+z);
        // console.log(that.data.ResList.result[y]);
      }
      that.setData({
        mTimuLIs: mTimuLIs,
        loadingHidetrue
      })
    },
      function (reason) {
        console.log(reason);
        that.setData({
          loadingHidetrue
        })
      })
  },
})

题目界面 detail.wxml

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

全局样式 app.wxss

.container {
 height:100%;
 flex1;
 display: flex;
 flex-direction: column;
 box-sizing: border-box;
 background-size100%;
} 
.item-view{
  padding10px;
  display: flex;
  flex-direction: column;
  border-top1px solid #DEDEDE;
  border-left1px solid #DEDEDE;
  box-shadow2px 2px 2px #C7C7C7;
  margin10px;
  border-radius5px;
}

.item-view .content{color: black;}
.item-view .datecolor: grey;margin-top10px;}
.item-view image{width100%;height400rpx;margin-top10rpx;}
.loading-view{display: flex;flex-direction: row; justify-content: center;align-items: center;padding10px;}



.timu{border1px solid #DFDFDF;margin20rpx;border-radius10px;}
.timu .title{font-size40rpx; }
.timu .question{text-indent20rpx;margin-left10rpx; padding10rpx;}
.timu .img{width100%;display:flex;flex-direction: column;align-items: center;margin0 auto;padding-top10rpx;padding-bottom10rpx;}
.timu .content{font-size30rpx;padding10rpx;margin-left20rpx }
.timu .select{font-size30rpx;margin-left30rpx;margin-right30rpx; padding20rpx; }

感谢各位的阅读!关于“微信小程序如何使用template模板”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×