欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序template模版的使用方法

 更新時(shí)間:2019年04月13日 07:53:57   作者:閑不住的李先森  
這篇文章主要給大家介紹了關(guān)于微信小程序template模版的使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用微信小程序具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

前言

小程序開(kāi)發(fā)語(yǔ)言雖然只能運(yùn)行在微信小程序中, 但是它的設(shè)計(jì)同樣遵循了主流前端框架的主要特征——組件化,在小程序中組件化的實(shí)現(xiàn)有兩種方式: template 模版 和 Component 組件。 這兩種方式分別適用于不同的場(chǎng)景。

  • template 模版 主要用于展示,模版中不涉及事件處理, 需要處理的事件邏輯放在調(diào)用模版的頁(yè)面中。 一個(gè) template 模版 只包含 wxml wxss 文件。
  • Component 組件 作為一個(gè)單獨(dú)的功能模塊,不僅可以包含頁(yè)面展示還可以包含該模塊的事件邏輯處理。 像一個(gè)頁(yè)面一樣, Component 組件 可以包含 wxml wxss js json 文件。

1. 創(chuàng)建 template 模版

不同于 page 和 Component 的創(chuàng)建, 在開(kāi)發(fā)者工具中并不能快速創(chuàng)建一個(gè) template 模版。所以需要單獨(dú)創(chuàng)建 wxss wxml 文件。

template.wxml 文件語(yǔ)法

一個(gè) template.wxml 文件中使用 <template> 標(biāo)簽包含一個(gè)模版, 一個(gè) template.wxml 文件可以包含多個(gè) <template>模版, 使用 name 屬性作為模版的名稱(chēng)。

在模版中可以接受變量, 使用 {{}} 展示。 為變量的傳遞者由調(diào)用該模版的頁(yè)面?zhèn)鬟f。

<template name="A">
 <text>template name: {{name}}</text>
</template>
<template name="B">
 <text>template name: {{name}} {{msg}}</text>
</template>

template.wxss 模版樣式文件

模版可以擁有自己的樣式文件

text{
 color: #cccccc;
}

2. 引用 template 模版

  • template 模版的引用需要使用 <import> 標(biāo)簽。 該標(biāo)簽的 src 屬性為需要引用模版的路徑。
  • template 模版的使用用 <template> 標(biāo)簽。 使用 is 屬性來(lái)區(qū)別模版文件中定義的模版。
  • 使用 data 傳入模版中的數(shù)據(jù)。

index.wxml

<import src="../tpls/template.wxml" />

<view>
 <template is="A" data="{{name}}"/>
 <template is="B" data="{{name, msg}}"/>
<view>

3. 引用模版樣式

在 調(diào)用頁(yè)面的 wxml 中引用了 template.wxml 后,模版的樣式并不會(huì)引用, 需要在調(diào)用頁(yè)面的 wxss 中單獨(dú)引用 template.wxss 文件。

index.wxss

@import "./tpls/template.wxss"

4. 模版文件中的事件處理

在模版中定義的事件, 需要調(diào)用頁(yè)面中執(zhí)行。

template.wxml

<template name="A">
 <text bindtap="handleTap">template name: {{name}}</text>
</template>

index.js

Page({
 data: {},
 handleTap() {
 console.log('template 模版 click')
 }
})

5.  import 有作用域

import 有作用域的概念,即只會(huì) import 目標(biāo)文件中定義的 template,而不會(huì) import 目標(biāo)文件中 import 的 template,簡(jiǎn)言之就是 import 不具有遞歸的特性。

例如:C 引用 B,B 引用A,在C中可以使用B定義的 template,在B中可以使用A定義的 template ,但是C不能使用A定義的template

6. include 配合 template 模版

如同使用 <import src="xx/xx/xx.wxml"> <tempalte is="A" /> 引用和使用模版一樣, 同樣也可以使用 <include src="xx/xx/xx.wxml />" 來(lái)引用一個(gè)模版。

需要注意的是:

  • 使用 <include> 引用模版文件時(shí), 并不能分別出模版文件的模版塊, 所以使用 <include> 引用的模版文件中只能定義一個(gè)模版塊。
  • include 可以將目標(biāo)文件中除了 <template/> <wxs/> 外的整個(gè)代碼引入,相當(dāng)于是拷貝到 include 位置。
<!-- index.wxml -->
<include src="header.wxml"/>

<view> body </view>

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

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論