小程式中引用import和include區別

來源:互聯網
上載者:User

小程式中有兩種引用方式一種是直接引用include,通過它相當於把<templete/>標註的以外的代碼全部copy過來當前位置,而import則是相反,它是只引用模板 import方式
這是index.wxml中的代碼

<!--src需要寫入相對路徑--><import src="../list/list.wxml"/><template is="listTemp"></template>

這是list.wxml中的代碼

<!--不會被引用--><view >listView</view><!--會被引用--><template name="listTemp"><view >我是模板</view></template><!--不會被引用--><template is="listTemp"></template>


import引用範圍是有限制的,引用頁面只能使用被引用頁面的模板,不能使用被引用頁面中引用其它頁面模板的模板
這是index.wxml中的代碼

<!--src需要寫入相對路徑引入list.wxml--><import src="../list/list.wxml"/><template is="listTemp"></template>

這是list.wxml中的代碼

<!--src需要寫入相對路徑引入logs.wxml--><import src="../logs/logs.wxml"/><template is="logsTemp"></template><!--不會被引用--><view >listView</view><!--會被引用--><template name="listTemp"><view >我是模板</view></template><!--不會被引用--><template is="listTemp"></template>

這是logs.wxml中的代碼

<!--不會被引用--><view >logsView</view><!--會被引用--><template name="logsTemp"><view >我是logs模板</view></template><!--不會被引用--><template is="logsTemp"></template>




可以看到index頁面只是引用了list中的模板並沒有引用logs的模板 include方式
index頁面

<!--src需要寫入相對路徑--><include src="../list/list.wxml"/>

list頁面

<!--會被引用--><view >listView </view><!--不會被引用--><template name="listTemp"><view >我是list模板:{{name}}</view></template><!--會被引用--><template is="listTemp" data="{{name}}"></template>


(注意這裡用到了name這個屬性,我在這裡測試了一下,在index.js的data中設定了name:“我是index資料來源”,在list.js中也配置了name:”我是list資料來源”,結果在引用的時候被引用頁面的資料來源總是取index.js中的資料,由此可見當用include時本路徑下的資料時不引用的,組後我又測試了下點擊事件等也都是不行的,可見無論是import方式還是include方式它就是一個引用,被引用的頁面所有的互動只和當前引用頁的邏輯js進行互動 )
最後總結就是import可以用來引用模板,在開發中可以避免相同模板的重複編寫,而include適合引入組件檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.