1、template的使用
WXML提供模板(template),可以在模板中定義代碼片段,然后在不同的地方調用。使用 name 屬性,作為模板的名字。然后在 <template/> 內定義代碼片段,如:
<template name="demo">
<view wx:for="{{items}}" wx:key="index">
{{item.value}}:{{item.name}}
</view>
</template>
在需要的地方使用,使用 is 屬性,聲明需要的使用的模板
<template is='demo' data='{{items}}'></template>
將模板所需要的 data 定義在相應的js的data中
items: [ {value: 'first', name: "局勢已無法挽回"}, {value: 'second', name: "人民有信仰,國家有力量,民族有希望"}, {value: 'third', name: "快出殘影"}, ]
2、引用
WXML 提供兩種文件引用方式import和include。
Import:
可以在該文件中使用目標文件定義的 template(wxml文件中)
<import src="../index/index"/>
include:
include 可以將目標文件中除了 <template/> <wxs/> 外的整個代碼引入,相當於是拷貝到 include 位置。
<include src="../index/index"/>
