小程序-引用的两种方式: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>

 

posted @   吴小明-  阅读(3326)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· 地球OL攻略 —— 某应届生求职总结
点击右上角即可分享
微信分享提示