微信小程序之组件模板(公用)引用

微信小程序之组件模板(公用)引用
关于引用的官方文档:
https://mp.weixin.qq.com/debug/wxadoc/dev/framework/view/wxml/import.html
①引用方法之include
include 可以将目标文件除了 外的整个代码引入,相当于是拷贝到 include 位置

这里写图片描述

建立一个公用模板页面
这里写图片描述

在wxml中写点东西

<text>1111111111111111111</text>

另一个页面准备引入这个组件
在想要引入的位置上

<include src="../publicsth/publicsth" />

完成
但是include对template模板就没有这个作用了

②引用方法之import
import可以在该文件中使用目标文件定义的template

在一个页面中写了两个模板

<template name="footer1">
2222222222
</template>
<template name="footer2">
2222222222333333333333333333
</template>

另一个页面想要引入它

<import src="../publicsth/footer" />
<template is="footer1"/>
<template is="footer2"/>

完成

注意:
import 的作用域
import 有作用域的概念,即只会 import 目标文件中定义的 template,而不会 import 目标文件 import 的 template。

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

猜你喜欢

转载自blog.csdn.net/cream_cicilian/article/details/79402984