在开发 微信应用号的过程当中,但愿可以达到这种效果;定义了多个通用的 wxml 来放置tempalte ,同时但愿有一个 wxml 可以统一的管理引用的模板;html
以下所示:微信
其中 input,wxml 代码为:模块化
<!-- 微信搜索框 --> <template name="searcheInputView1"> <view class="section weixin-search-input-view page-view-direction-row"> <input placeholder="搜索" /> </view> </template> <template name="searcheInputView2"> <view class="section weixin-search-input-view page-view-direction-row"> <input placeholder="搜索" /> </view> </template>
包含了不少input类型的模板 commonTemplate.wxml 但愿可以引入input.wxml、panel.wxml 等,而后再具体的页面中只须要引入commonTemplate.wxml 便可用到全部定义的模板测试
当看到微信应用号引用时,发现import 只可以传递一层,即 A->B B->C A !->C 这样就致使了这种方法不能使用。 蛋疼之余,看到include 是这样描述的code
include能够将目标文件出了<template/>的整个代码引入,至关因而拷贝到include位置xml
那么 <import>算是 template 的内容吗?测试一下,结果惊喜的发现居然是能够这么使用的。因而十分兴高采烈的使用上了个人 ”组件模块化” 。 最后放出代码: commonTemplate.wxmlhtm
<!-- 该文件作通用引用功能 --> <!-- 输入框组件 --> <import src="input.wxml" /> <!-- 面板组件 --> <import src="panel.wxml" />
模板代码 : input.wxml图片
<!-- 微信搜索框 --> <template name="searcheInputView1"> <view class="section weixin-search-input-view page-view-direction-row"> <input placeholder="搜索" /> </view> </template> <template name="searcheInputView2"> <view class="section weixin-search-input-view page-view-direction-row"> <input placeholder="搜索" /> </view> </template>
调用代码: weixin.wxml开发
<!-- 导入模板--> <include src="../../common/template/commonTemplate.wxml" /> <!--一个块级区域--> <template is="searcheInputView" />
完美解决问题,不过这里可能会引发效率问题,由于加载太多的模板,这里待研究测试get