小程序-引用的两种方式:import和include

import

  import可以在该文件中使用目标文件定义的template,如:

    在mine.wxml中定义了一个叫item的template:
      <template name="item">
        <text>{{text}}</text>
      </template>
    在detail.wxml中引用了mine.wxml,就可以使用item模板:
      <import src="../mine/mine.wxml"/>
      <template is="item" data="{{text:'设置了一些内容111'}}"></template>
 
  import的作用域:
    import有作用域的概念,即只会import目标文件中定义的template,而不会import目标文件import的template。

如:C import B,B import A,在C中可以使用B定义的template,在B中可以使用A定义的template,但是C不能使用A定义的template。
<!-- A.wxml -->
<template name="A">
<text>A 模板</text>
</template>

<!-- B.wxml -->
<import src="A.wxml">
<template name="B">
<text>B 模板</text>
</template>

<!-- C.wxml -->
<import src="B.wxml">
<template is="A"/> <!-- 报错:没有import A模板时,不能使用A模板 -->
<template is="B"/>
 
 

include

  

  include可以将目标文件除了<template/> <wxs/>外的整个代码引入,相当于是拷贝到include位置,如:
       <!-- index.wxml -->
      <include src="header.wxml"/>
      <view>body</view>
      <include src="fotter.wxml"/>

<!-- header.wxml -->
<view>header</view>

<!-- fotter.wxml -->
<view>fotter</view>

猜你喜欢

转载自www.cnblogs.com/wuqilang/p/12076106.html
今日推荐