首頁  >  文章  >  微信小程式  >  微信小程式之引用教程

微信小程式之引用教程

高洛峰
高洛峰原創
2018-05-15 10:20:392407瀏覽

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

系列文章:

微信小程式教程之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可以將目標檔案出了dcdc0fa59b5fea5bdae0d810c3919fcd的整個程式碼引入,相當於是拷貝到include位置,如:

<!-- index.wxml -->
<include src="header.wxml"/>
<view> body </view>
<include src="footer.wxml"/>
<!-- header.wxml -->
<view> header </view>
<!-- footer.wxml -->
<view> footer </view>

感謝閱讀,希望能幫助大家,謝謝大家對本站的支持!

以上是微信小程式之引用教程的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn