美文网首页
小程序中模板的使用

小程序中模板的使用

作者: GaoXiaoGao | 来源:发表于2020-12-09 13:15 被阅读0次

    使用模板可以减少重复代码的使用,使用方法如下

    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

    相关文章

      网友评论

          本文标题:小程序中模板的使用

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