引用 import和include

官方文档:https://developers.weixin.qq.com/miniprogram/dev/reference/wxml/import.html

总结1、如果想引用一片自己定义的外部模板(template),则可以使用  import ,它会在 当前文件中 使用 目标文件 里定义的 template,

举例:

步骤1、先在单独的.wxml中,写好模板

<template name="newA">
  <view>
    <text> 余额: {{remaining}}</text>
    <text> 时间: {{time}}</text>
  </view>
</template>

步骤2、在需要的地方使用 import ,并  渲染模板和数据(如果没有渲染模板则不会显示)

<import src="a.wxml"/>
<template wx:for="{{array}}" is="newA" data="{{...array[index]}}"/>

注意:import 的作用域

import 有作用域的概念,即只会 import 目标文件中定义的模板(template),而不会 import 目标文件 import 的模板(template)

如:C import B,B import A,在C中可以使用B定义的template,在B中可以使用A定义的template,但是C不能使用A定义的template

猜你喜欢

转载自www.cnblogs.com/caitangbutian/p/11963408.html