在開發 微信應用號的過程當中,但願可以達到這種效果;定義了多個通用的 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