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"/>