美文网首页
小程序代码复用 - template

小程序代码复用 - template

作者: 十毛tenmao | 来源:发表于2020-09-19 23:49 被阅读0次

为了提高代码的复用率,小程序提供了多种代码复用的模式,其中最简单的就是模板template。本文介绍一下如何定义模板及使用。

template定义

<template name="easy">
  <text class='red' data-name="{{name}}" bindtap="click">I'm Easy,{{greeting}}</text>  
</template>

<template name="Davi">
  <text>I'm Davi,nice to meet you</text>
</template>

使用data-name可以在事件中传递参数name给处理函数click

template使用

<import src="../../template/template.wxml" />

<view class="container">
  <!-- 引用template模板,用is="easy"的方式,选择模板 -->
  <template is="easy"  data="{{...person}}" />
  <template is="Davi" />
</view>

数据及事件处理

//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    person: {
      greeting: "你好",
      name: "tim"
    }
  },
  //事件处理函数
  click: function(e) {
    console.log(e);
    console.log(e.currentTarget.dataset.name);
  }
})

输出自定义参数e.currentTarget.dataset.name

注意事项

  • template并没有定义事件回调函数,需要在使用方定义(如果要有自定义的函数,可以使用小程序组件component)

参考

相关文章

  • 小程序代码复用 - template

    为了提高代码的复用率,小程序提供了多种代码复用的模式,其中最简单的就是模板template。本文介绍一下如何定义模...

  • 微信小程序代码复用技术-模板/组件/插件

    微信小程序代码复用技术-模板/组件/插件 MD by Jimbowhy and you can visit my ...

  • template模板

    有时候需要复用页面的某一块区域,包括样式,可以考虑template template.wxml template....

  • 面试底层分享(二)

    什么是库?现有的、成熟的可以复用的代码,程序代码的集合,共享程序代码的一种方式。 库的分类根据程序代码的开源情况,...

  • template标签的使用

    template标签,主要用于公用组件内容,方便调用,提高了代码的复用性 一层template 1. 先新建tem...

  • 使用framework打包静态库

    1、认识静态库和动态库 首先 库 是对程序代码、函数及资源文件的封装,是一种共享程序代码,实现代码资源复用的方式。...

  • pyhton代码复用与递归

    代码复用:把代码当成资源进行抽象,函数和对象是代码复用的两种主要形式 -代码资源化:程序代码是一种用来表达计算的资...

  • Django Template继承

    Templates继承可实现模板复用,公用区域统一配置,减少代码量,更容易更新和维护。 template-inhe...

  • vue slot 插槽的使用和理解

    元素作为承载分发内容的出口,可以实现组件的复用。 简单的说就是《定制模板》 一个template由多...

  • Vue开发技巧总结

    本文目录: 1.sync优雅更新props 2.卸载watch观察 3.巧用template 4.过滤器复用 5....

网友评论

      本文标题:小程序代码复用 - template

      本文链接:https://www.haomeiwen.com/subject/cftboqtx.html