import和include作用域——微信小程序

2023-05-16,,

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 template </text></template><!-- B.wxml --><import src="a.wxml"/><template name="B">
  <text> B template </text></template><!-- C.wxml --><import src="b.wxml"/><template is="A"/>  <!-- Error! Can not use tempalte when not import A. --><template is="B"/>

include 的作用域


include可以将目标文件除了<template/>的整个代码引入,相当于是拷贝到include位置,并可嵌套引入,如:

如:C include B,B include A,等于 C include B和C。

<!-- A.wxml --><view> A </view><!-- B.wxml --><include src="A.wxml"/><view> B </view><!-- C.wxml --><include src="B.wxml"/><view> C </view>

文/JWang_小猴土豆(简书作者)
原文链接:http://www.jianshu.com/p/1d14d7f6031c
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。