使用模板可以减少重复代码的使用,使用方法如下
1.使用方法
//定义模板
<template name="content">
<button size="mini">{{btnText}}</button>
<view>{{viewText}}</view>
</template>
<!-- 使用模板,通过data="{{key:value,key:value}}"形式设置参数的值 -->
<template is="content" data="{{btnText:'按钮1',viewText:'内容1'}}"></template>
<template is="content" data="{{btnText:'按钮2',viewText:'内容2'}}"></template>
2.使用方法
在单独的文件中定义好模板
通过import导入并使用
<!-- 使用绝对路径 -->
<import src="/pages/wxml/template.wxml"/>
<!-- 使用相对路径
<import src="./template.wxml"/> -->
<!-- 通过data="{{key:value,key:value}}"形式设置参数的值 -->
<template is="content" data="{{btnText:'按钮1',viewText:'内容1'}}"></template>
<template is="content" data="{{btnText:'按钮2',viewText:'内容2'}}"></template>
代码参考
https://github.com/LongLongLongLongLongLongLongLongLong/LearnMiniProgram.git
网友评论