微信小程式之引用教程

高洛峰
發布: 2018-05-15 10:20:39
原創
2406 人瀏覽過

這篇文章主要介紹了微信小程式引用的相關資料,並附簡單實例程式碼,需要的朋友可以參考下

系列文章:

微信小程式教程之WXSS
微信小程式教學之引用
微信小程式教學之事件
微信小程式教學範本
微信小程式教學課程清單渲染
微信小程式教學條件渲染
#微信小程式教學之資料綁定
微信小程式教學之WXML

引用

WXML提供兩種檔案參考方式import和include。

import

import可以在該檔案中使用目標檔案定義的template,如:

在item.wxml中定義了一個叫item的template:

<!-- item.wxml -->
<template name="item">
 <text>{{text}}</text>
</template>
登入後複製

在index.wxml中引用了item.wxml,就可以使用item模板:

<import src="item.wxml"/>
<template is="item" data="{{text: &#39;forbar&#39;}}"/>
登入後複製

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可以將目標檔案出了