WXML導入的兩種方式

wxml導入的兩種方式:

  • import方式
  • include方式

模板和模板導入:
模板現在用的很少了,因爲現在有自定義組件了。

<!-- 13.1 定義模板 -->
<template name="contentItem">
  <button>click me</button>
  <view>呵呵</view>
</template>

<!-- 13.2 使用模板 -->
<template is="contentItem" />

顯示的內容不確定,我可以自己決定要顯示啥,就用{{}}和data

<!-- 顯示的內容不確定 -->
<template name="contentItem">
  <button>{{btnText}}</button>
  <view>{{content}}</view>
</template>

<template is="contentItem" data="{{btnText: '自定義button111', content: '自定義text111'}}" />
<template is="contentItem" data="{{btnText: '自定義button222', content: '自定義text222'}}" />
<template is="contentItem" data="{{btnText: '自定義button333', content: '自定義text333'}}" />

如果我的模板想在其他頁面中使用,做法:

  • 先將template代碼抽取到一個單獨的wxml文件中
  • 用<import />組件來導入這個template
  • 使用
創建 /wxml/template.wxml 文件,並將template代碼引入

在其他頁面 導入template的代碼,絕對路徑和相對路徑都可以
<import src="/wxml/template.wxml" />

使用
<template is="contentItem" data="{{btnText: '自定義button111', content: '自定義text111'}}" />

特點:不能循環導入
什麼意思:不能在模板a中導入另一個模板b,然後在頁面中導入模板a,這樣做是不會顯示b的內容的。

include方式導入

include的導入方式,會導入除了<template><wxs>之外的代碼
比如有個header.wxml和footer.wxml,就可以導入這倆了。

header/wxml:
<view>我是header</view>

footer.wxml:
<view>我是footer</view>

<!-- include導入方式 -->
home.wxml:
<include src="/wxml/header.wxml" />
<include src="/wxml/footer.wxml" />

特點:include是可以循環導入的
比如我可以將A頁面導入到B頁面,並將B頁面導入到C頁面。這時候C頁面會將AB的代碼都加載進去。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章