模板WXML提供模板(template),可以在模板中定义代码片段,然后在不同的地方调用。当项目需要多次使用同一个布局和样式的时候,就可以考虑使用template(模板)来减少冗余代码。
如何定义以及使用模板
wxml模块:
新建template文件夹,新建wxml。wxml文件由template标签包裹,并设置template标签名字属性方便调用。使用时在对应wxml文件通过import引用。templatename="postitem"viewclass='post-container'viewclass='post-author-date'imageclass='post-author'src='/images/xml.jpg'/imagetextclass='post-date'{{item.date}}/text/viewtextclass='post-title'{{item.title}}/textimagewx:if="{{!boolean_show}}"class='post-image'src="{{item.image}}"/imagetextclass='post-content'{{item.content}}/textviewclass='post-like'imageclass='post-like-image'src="{{item.img.view_img}}"/imagetextclass='post-like-font'{{item.view_num}}/textimageclass='post-like-image'src="{{item.img.collect_img}}"/imagetextclass='post-like-font'{{item.collect_num}}/text/view/view/template
引用template模板的语句实际上只有下面一句,后面加上data属性是为了遍历数组得到数组中的每一个元素对象的每个内容
templateis="postitem"/
importsrc="文件路径"/!-在需要调用处,通过名字属性调用--blockwx:for="{{post_key}}"wx:for-item="item"!--将postId作为一个属性存放到view标签中,自定义属性data开头--viewcatchtap='onPostTap'data-postId="{{item.postId}}"templateis="postitem"data="{{item}}"//view/block
wxss模块:
1.在template文件夹下新建wxss文件。
2.写入wxss内容。
3.使用时在对应wxml文件通过@import引用。
.post-container{/弹性盒子模型/display:flex;flex-direction:column;margin-top:20rpx;margin-bottom:40rpx;background-color:#fff;/上下边界/border-bottom:1pxsolid#ededed;border-top:1pxsolid#ededed;/页边距/padding-bottom:5px;}
引用语句只有一句,不需要通过import引用
@import"文件路径";
下面的wxss文件中就已经引用了template文件夹下的wxss文件的内容了,比如上面的.post-container
@import"文件路径";swiper{width:100%;height:600rpx;}swiperimage{width:100%;height:600rpx;}
最新资讯
-

小程序制作平台选型踩坑记录:2026年五大主流方案横向对比
2026 年微信小程序月活达 10.7 亿、覆盖 108 个行业,本次横向对比即速应用、乔拓云、凡科、有赞、微盟五大主流平台,分三阶段给出选型结论,核心聚焦成本、扩展性、运营能力三大维度。 -

即速应用,赋能企业玩转微信小程序智慧经营
作为国内领军的智慧商业经营服务商,即速应用始终秉承“让每个企业都拥有自己的智慧店铺”的愿景,持续赋能更多企业玩转智慧经营。即速应用旗下拥有“小程序搭建工具-即速应用”、“私域流量专家-即客云”等产品,帮助商家打通互联网全生态营销闭环。 -

即客云2.0重磅更新,让微信小程序运营更简单!
即客云作为一款基于企业微信的第三方工具,现从多维度提供超过30种功能,自上线以来,已服务多家企业,受到一致好评。近期,我们根据客户反馈和市场调研正式推出升级版 即客云2.0!更新了私域运营SOP,群日历功能,批量拓客,客户雷达,消息推送,个人欢迎语,帮助企业更好运用企业微信;同时提升了社群运营工作标准化,提升运营效率,帮助企业实现客户增长,玩转私域流量。










