微信小程序的template

一,使用场景

关于微信小程序中template的使用场景是:
1.需要将页面部分进行提取
2.不涉及到逻辑处理,仅作展示作用
判断是采用组件的方式还是使用template的方式的依据是是否含有逻辑处理,含有逻辑处理则选择组件,仅作展示的选择template

二、template的基础使用

步骤:
1.创建模板

//template.wxml
<template name="templateName">
//内容
  ...
</template>

2.创建模板样式

//template.wxss
view{
  background: red;
}

3.在需要使用模板的页面引入

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

4.使用模板

<template is="templateName" />

5.模板样式导入(在页面的wxss文件中导入)

//a.wxss
@import "./template.wxss";

6.数据传递
6.1在模板中定义参数

//template.wxml
<template name="templateName">
  <text>{{username}}</text>
</template>

6.2参数由外部传入

//a.wxml
<template is="templateName" data="{{username: 'whitemu'}}" />
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容