亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解微信小程序入門(mén)五: wxml文件引用、模版、生命周期

 更新時(shí)間:2017年01月20日 08:59:01   作者:lecepin  
本篇文章主要介紹了詳解微信小程序入門(mén)五: wxml文件引用、模版、生命周期,具有一定的參考價(jià)值,有興趣的可以了解一下。

實(shí)例一: include方式引用header.wxml文件

文件引用對(duì)于代碼的重用非常重要,例如在web開(kāi)發(fā)中我們可以將公用的header部分和footer等部分進(jìn)行提取,然后在需要的地方進(jìn)行引用。

微信小程序里面,是包含引用功能的——include、import。這兩個(gè)引用文件的標(biāo)簽,使用基本差不多,這里先說(shuō)一下include。

微信中的視圖文件引用,引用過(guò)來(lái)的都是沒(méi)有渲染的,基本類似于直接將引用過(guò)來(lái)的文件復(fù)制到引用位置,所以我們需要重新對(duì)其渲染。

實(shí)例說(shuō)明

這里將默認(rèn)創(chuàng)建的用戶頭像信息提取出到header.wxml中,做為頭部引用,分別由index2.wxml和index3.wxml引用,引用方式為include。

實(shí)例代碼

在pages中創(chuàng)建common/header.wxml

從index.wxml中將系統(tǒng)默認(rèn)創(chuàng)建的用戶信息結(jié)構(gòu)復(fù)制到header.wxml中。

header.wxml代碼:

<!--pages/common/header.wxml-->
 <view bindtap="bindViewTap" class="userinfo">
  <image class="userinfo-avatar" src="{{userInfo.avatarUrl}}" background-size="cover"></image>
  <text class="userinfo-nickname">{{userInfo.nickName}}</text>
 </view>

因?yàn)閮蓚€(gè)頁(yè)面都要包含header.wxml,所以樣式文件就不重復(fù)寫(xiě)了,這里直接將樣式拷貝到app.wxss。

app.wxss代碼:

/**app.wxss**/
.userinfo {
 display: flex;
 flex-direction: column;
 align-items: center;
}

.userinfo-avatar {
 width: 128rpx;
 height: 128rpx;
 margin: 20rpx;
 border-radius: 50%;
}

.userinfo-nickname {
 color: #aaa;
}

創(chuàng)建index/index2和index/index3

index2.wxml內(nèi)容:

<!--pages/index/index2.wxml-->
<view class="container">
  <include src="../common/header.wxml" />

  <view class="myBtn">
    <button type="primary" bindtap="goIndex3">進(jìn)入index3</button>
  </view>  
</view>

因?yàn)閕ndex2.wxml和index3.wxml都需要userInfo數(shù)據(jù),所以這邊在index2獲取到數(shù)據(jù)后,使用本地存儲(chǔ)進(jìn)行存儲(chǔ),index3.wxml讀取本地存儲(chǔ)。

index2.js代碼:

// pages/index/index2.js
var app = getApp()

Page({
 data: {
  userInfo: {},
 },

 goIndex3:function(){
  wx.navigateTo({
   url: 'index3'
  })
 },

 onLoad: function () {
  console.log('onLoad')
  var that = this
  app.getUserInfo(function (userInfo) {
   that.setData({
    userInfo: userInfo
   })

   //本地存儲(chǔ)
   wx.setStorageSync('userInfo', userInfo)
  })
 }
})

index3.wxml代碼:

<!--pages/index/index3.wxml-->

<view class="container">
  <include src="../common/header.wxml" />

  <text>pages/index/index3.wxml</text>
</view>

index3.js代碼:

// pages/index/index3.js
Page({
 data:{
  userInfo: {},
 },
 onLoad:function(options){  
  this.setData({
   userInfo: wx.getStorageSync('userInfo')
  })
 },
})

實(shí)例效果

 

實(shí)例二: import方式引用footer.wxml文件

這個(gè)實(shí)例使用import來(lái)引用文件,import比include要強(qiáng)大的多,待會(huì)我再對(duì)于這兩都進(jìn)行一下對(duì)比。

import引用方式涉及到了微信的模版(template),這里先說(shuō)一下template。

微信視圖模版(template)

template也是寫(xiě)在.wxml中,然后使用<template>...</template>標(biāo)記指定模版信息,模版下定義:

<template name="msgItem">
  視圖代碼...
</template>

使用name屬性,作為模板的名字。

使用模版:

<template is="msgItem" data="{{...item}}"/>

data為向模版?zhèn)魅氲臄?shù)據(jù)。

實(shí)例說(shuō)明

使用模版的方式創(chuàng)建footer視圖代碼片,然后用import和template進(jìn)行代碼的調(diào)用。

實(shí)例代碼

創(chuàng)建footer.wxml

  

footer.wxml代碼:

  

index2.wxml代碼:

  

實(shí)例效果

  

實(shí)例三: 小程序退出時(shí)清除本地?cái)?shù)據(jù)

這里涉及到了小程序的生命周期問(wèn)題,可以類比一下安卓生命周期,小程序的生命周期在app.js中進(jìn)行定義:

屬性 類型 描述 觸發(fā)時(shí)機(jī)
onLaunch Function 生命周期函數(shù)–監(jiān)聽(tīng)小程序初始化 當(dāng)小程序初始化完成時(shí),會(huì)觸發(fā) onLaunch(全局只觸發(fā)一次)
onShow Function 生命周期函數(shù)–監(jiān)聽(tīng)小程序顯示 當(dāng)小程序啟動(dòng),或從后臺(tái)進(jìn)入前臺(tái)顯示,會(huì)觸發(fā) onShow
onHide Function 生命周期函數(shù)–監(jiān)聽(tīng)小程序隱藏 當(dāng)小程序從前臺(tái)進(jìn)入后臺(tái),會(huì)觸發(fā) onHide
onError Function 錯(cuò)誤監(jiān)聽(tīng)函數(shù) 當(dāng)小程序發(fā)生腳本錯(cuò)誤,或者 api 調(diào)用失敗時(shí),會(huì)觸發(fā) onError 并帶上錯(cuò)誤信息

page生命周期:

屬性 類型 描述
onLoad Function 生命周期函數(shù)–監(jiān)聽(tīng)頁(yè)面加載
onReady Function 生命周期函數(shù)–監(jiān)聽(tīng)頁(yè)面初次渲染完成
onShow Function 生命周期函數(shù)–監(jiān)聽(tīng)頁(yè)面顯示
onHide Function 生命周期函數(shù)–監(jiān)聽(tīng)頁(yè)面隱藏
onUnload Function 生命周期函數(shù)–監(jiān)聽(tīng)頁(yè)面卸載

這里用到了onUnload事件。

index2.js代碼:

  

include 與 import

import可以在該文件中使用目標(biāo)文件定義的template

include可以將目標(biāo)文件除了<template/>的整個(gè)代碼引入,相當(dāng)于是拷貝到include位置

import的作用域

import有作用域的概念,即只會(huì)import目標(biāo)文件中定義的template,而不會(huì)import目標(biāo)文件import的template。

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論