小程序使用模板template

Posted on 2020-05-18 13:56  张雪冬前端学习园地  阅读(236)  评论(0编辑  收藏  举报

小程序使用模板template

 

1.介绍模板就是代码的高度复用,将在很多页面使用了相同的部分可以使用模板封装

<!-- 在页面组件中使用 -->

<!-- 此时定义了一个模板 -->
<template name="staffName">
  <view>
    FirstName: {{firstName}}, LastName: {{lastName}}
  </view>
</template>


<!-- 通过is属性指定使用哪个模板,...staffA是传入的数据 -->
<template is="staffName" data="{{...staffA}}"></template>


<!-- 通过外链使用,根目录下新建templates目录,再新建的demo目录,目录下有index.wxml和index.wxss -->

<!-- 在页面组件中引入使用 -->

<import src="../../templates/demo/index.wxml" />

<template is="last" data="{{msg}}"></template>