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

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

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

前言

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

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

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

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

template.wxml 文件語法

一個 template.wxml 文件中使用 <template> 標簽包含一個模版, 一個 template.wxml 文件可以包含多個 <template>模版, 使用 name 屬性作為模版的名稱。

在模版中可以接受變量, 使用 {{}} 展示。 為變量的傳遞者由調(diào)用該模版的頁面?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> 標簽。 該標簽的 src 屬性為需要引用模版的路徑。
  • template 模版的使用用 <template> 標簽。 使用 is 屬性來區(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)用頁面的 wxml 中引用了 template.wxml 后,模版的樣式并不會引用, 需要在調(diào)用頁面的 wxss 中單獨引用 template.wxss 文件。

index.wxss

@import "./tpls/template.wxss"

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

在模版中定義的事件, 需要調(diào)用頁面中執(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 有作用域的概念,即只會 import 目標文件中定義的 template,而不會 import 目標文件中 import 的 template,簡言之就是 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 />" 來引用一個模版。

需要注意的是:

  • 使用 <include> 引用模版文件時, 并不能分別出模版文件的模版塊, 所以使用 <include> 引用的模版文件中只能定義一個模版塊。
  • include 可以將目標文件中除了 <template/> <wxs/> 外的整個代碼引入,相當于是拷貝到 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é)

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

相關文章

最新評論