微信小程序开发之模板

1、简介

WXML提供模板(template),能够在模板中定义代码片断,而后在不一样的地方调用。html

定义模板

使用name属性,做为模板的名字。而后在<template/>内定义代码片断,如:小程序

<!-- index: int msg: string time: string --> <template name="msgItem"> <view> <text> {{index}}: {{msg}} </text> <text> Time: {{time}} </text> </view> </template> 

使用模板

使用 is 属性,声明须要的使用的模板,而后将模板所须要的 data 传入,如:微信小程序

<template is="msgItem" data="{{...item}}"/> 
Page({
  data: {
    item: {
      index: 0, msg: 'this is a template', time: '2016-09-15' } } }) 

is 属性可使用 Mustache 语法,来动态决定具体须要渲染哪一个模板:微信

<template name="odd"> <view> odd </view> </template> <template name="even"> <view> even </view> </template> <block wx:for="{{[1, 2, 3, 4, 5]}}"> <template is="{{item % 2 == 0 ? 'even' : 'odd'}}"/> </block> 

模板的做用域

模板拥有本身的做用域,只能使用data传入的数据。app

 

2、封装消息模板

1.功能需求

     小程序的wx.showToast接口只提供了两种icon【success和loading】展现形式,xss

  那假如我想要的是不要图标只要存粹的文字提醒呢?或者是我不要微信单方面提供的那种图片呢?想要本身指定的状况呢?这时候要怎么办..ide

       根据wx.showToast接口提供的参数,实现一下消息提醒模板函数

一、若是没有指定icon图标地址,那么就是单纯的显示文字提示,不然就是图标+文字的模式,这时候就要确保icon指向的图片地址是正确的啦。post

二、若是没有指定duration提示的延迟时间,默认是1.5s,而我设置的最大值10s是不会自动隐藏消息提示的,除非手动hideToast. 单位:毫秒测试

三、若是没有指定mask遮罩,默认是跟着显示的,防止触摸穿透

四、若是没有指定cb回调函数,默认直接显示消息提醒,不然能够在等消息提示结束后即刻处理一些其余业务:例如地址跳转,改变订单状态等等

2.模板代码

代码文件结构

images
    |--msg_info.png
pages
    |--index
        |--index.wxml 
        |--index.wxss 
        |--index.js 
template
    |--showToast.wxml 
    |--showToast.wxss
utils
    |--showToast.js

showToast.wxml代码

注:name可自定义,通过测试,它能够和文件名不一样

<template name="showToast">
  <block wx:if="{{showToast.isShow? showToast.isShow: false}}">
    <view class="toast-bg" wx:if="{{showToast.mask==false? false : true}}"></view>
    <view class="toast-center">
      <view class="toast">
        <image class="toast-icon" src="{{showToast.icon}}" mode="scaleToFill" wx:if="{{showToast.icon}}" />
        <text class="toast-text">{{showToast.title}}</text>
      </view>
    </view>
  </block>
</template>

showToast.wxss

注:它并不会自动引用,

能够在index.wxss的头部加入 @import "../../template/showToast.wxss"; 

也能够直接写在app.wxss中

/*showToast*/
.toast-bg {
  position: fixed;
  top: 0;
  bottom: 0;
  z-index: 999999;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, .2);
}
/*水平居中必备样式*/
.toast-center {
  position: fixed;
  z-index: 9999999;
  width: 100%;
  height: 100%;
  text-align: center;
}
.toast {
  display: inline-block;
  padding: 20rpx 40rpx;
  max-width: 600rpx;
  font-size: 28rpx;
  color: #fff;
  background: rgba(0, 0, 0, .5);
  border-radius: 10rpx;
  text-align: center;
}
/*垂直居中必备样式*/
.toast-center::after{
  content: '';
  display: inline-block;
  width: 0;
  height: 100%;
  vertical-align: middle;
}
.toast .toast-icon {
  display: block;
  margin: 0 auto 10rpx auto;
  width: 50rpx;
  height: 50rpx;
}

showToast.js

/*
显示toast提示
title:    提示的内容 必填
icon:     图标,//请指定正确的路径,选填
duration: 提示的延迟时间,单位毫秒,默认:1500, 10000永远存在除非手动清除 选填
mask:     是否显示透明蒙层,防止触摸穿透,默认:true 选填
cb:       接口调用成功的回调函数 选填
 */
function showToast(obj) {
    if (typeof obj == 'object' && obj.title) {
        if (!obj.duration || typeof obj.duration != 'number') { obj.duration = 1500; }//默认1.5s后消失
        if(obj.icon=='info'){obj.icon = '/images/msg_info.png';}
        else if(obj.icon=='error'){obj.icon = '/images/msg_error.png';}
        var that = getCurrentPages()[getCurrentPages().length - 1];//获取当前page实例
        obj.isShow = true;//开启toast
        if (obj.duration < 10000) {
            setTimeout(function () {
                obj.isShow = false;
                obj.cb && typeof obj.cb == 'function' && obj.cb();//若是有成功的回调则执行
                that.setData({
                    'showToast.isShow': obj.isShow
                });
            }, obj.duration);
        }
        that.setData({
            showToast: obj
        });
    } else if (typeof obj == 'string') {
        var that = getCurrentPages()[getCurrentPages().length - 1];//获取当前page实例
        var objData = { title: obj, duration: 1000, isShow: true };
        setTimeout(function () {
            objData.isShow = false;
            that.setData({
                showToast: objData
            });
        }, objData.duration);
        that.setData({
            showToast: objData
        });
    } else {
        console.log('showToast fail:请确保传入的是对象而且title必填');
    }
}
/**
 *手动关闭toast提示
 */
function hideToast() {
    var that = getCurrentPages()[getCurrentPages().length - 1];//获取当前page实例
    if (that.data.showToast) {
        that.setData({
            'showToast.isShow': false
        });
    }
}
module.exports = {
    showToast: showToast,
    hideToast: hideToast
}

 

3.模板的引用

这里将在index页面进行测试

index.wxml

<import src="../../template/showToast.wxml" />
<template is="showToast" data="{{showToast: showToast}}" />
<!--上面两句话是放置模板的路径和传入的data!   data传入方式写死固定-->
<view bindtap="testToast" data-test="1">只传title,单纯文字提醒</view>
<view bindtap="testToast" data-test="2">指定图标,图标+文字提醒</view>
<view bindtap="testToast" data-test="3">指定duration,控制toast 3s消失</view>
<view bindtap="testToast" data-test="31">指定duration=10s,手动2s后关闭toast</view>
<view bindtap="testToast" data-test="4">指定mask,控制toast遮罩</view>
<view bindtap="testToast" data-test="5">指定cb, 控制回调处理业务</view>

index.js

var feedbackApi=require('../../common/showToast');//引入消息提醒暴露的接口
Page({  
  .....//其余省略
  testToast: function(e){
    var test=e.target.dataset.test;
    if(test==1){
      feedbackApi.showToast({title: 'test shoToast title'})//调用
    }
    if(test==2){
      feedbackApi.showToast({
        title: 'test shoToast title',
        icon: '/images/msg_info.png'
        })
    }
    if(test==3){
      feedbackApi.showToast({
        title: 'test shoToast title',
        duration: 3000        
        })
    }
    if(test==31){
      feedbackApi.showToast({
        title: 'test shoToast title',
        duration: 10000        
        })
        setTimeout(function(){
          feedbackApi.hideToast();
        }, 2000)
    }
    if(test==4){
      feedbackApi.showToast({
        title: 'test shoToast title',
        mask: false
        })
    }
    if(test==5){
      feedbackApi.showToast({
        title: 'test shoToast title',
        cb: function(){
          console.log('回调进来了,能够制定业务啦')
        }
        })
    }
  }
})  

 其余用法

//扩展一
feedbackApi.showToast('test'); 
//扩展二
feedbackApi.showToast({
  title: 'test shoToast title',
  icon:'info' //'error'
});

 

 

 

参考:http://blog.csdn.net/eadio/article/details/54616595

 

欢迎阅读本系列文章:微信小程序开发教程目录

相关文章
相关标签/搜索