字節(jié)跳動小程序開發(fā)框架 Component 構(gòu)造器

2019-08-05 10:27 更新

Component 構(gòu)造器可用于定義組件,調(diào)用 Component 構(gòu)造器時可以指定組件的屬性、數(shù)據(jù)、方法等

定義參數(shù)


選項(xiàng)名 類型 是否必填 說明 最低版本
properties Object 組件的對外屬性,是屬性名到屬性設(shè)置的映射表,屬性設(shè)置中可包含三個字段,type 表示屬性類型、value 表示屬性初始值、observer 表示屬性值被更改時的響應(yīng)函數(shù)
data Object 組件的內(nèi)部數(shù)據(jù),和 properties 一同用于組件的模版渲染
methods Object 組件的方法,包括事件響應(yīng)函數(shù)和任意的自定義方法,關(guān)于事件響應(yīng)函數(shù)的使用,參見組件事件
behaviors String/Array 類似于 mixins
created Function 組件生命周期函數(shù),在組件實(shí)例進(jìn)入頁面節(jié)點(diǎn)樹時執(zhí)行,注意此時不能調(diào)用setData
attached Function 組件生命周期函數(shù),在組件實(shí)例進(jìn)入頁面節(jié)點(diǎn)樹時執(zhí)行
ready Function 組件生命周期函數(shù),在組件布局完成后執(zhí)行
moved Function 組件生命周期函數(shù),在組件實(shí)例被移動到節(jié)點(diǎn)樹另一個位置時執(zhí)行
detached Function 組件生命周期函數(shù),在組件實(shí)例被從頁面節(jié)點(diǎn)樹移除時執(zhí)行
relations Object 組件間的關(guān)系定義,參加組件間關(guān)系 1.12.0.0
options Object 其他選項(xiàng)
lifetimes Object 組件生命周期聲明對象,優(yōu)先級比同級的 attached、detached 等生命周期優(yōu)先級高
definitionFilter Function 定義過濾器,用于自定義組件擴(kuò)展,參見自定義組件擴(kuò)展

生成的組件實(shí)例可以在組件的方法、生命周期函數(shù)和屬性 observer 中通過 this 訪問。組件包含一些通用屬性和方法。

通用屬性列表

屬性名 類型 說明
is String 組件的文件路徑
id String 節(jié)點(diǎn) id
dataset String 節(jié)點(diǎn) dataset
data Object 組件數(shù)據(jù)
properties Object 組件數(shù)據(jù)

方法列表

方法名 參數(shù)及其類型 說明 最低版本
setData Object data 設(shè)置 data
hasBehavior Object behavior 檢查組件是否具有 behavior
triggerEvent String eventName, Object detail, Object options 觸發(fā)事件
selectComponent String selector, Function callback 使用選擇器選擇組件實(shí)例節(jié)點(diǎn),返回匹配到的第一個組件實(shí)例對象,此方法為異步方法,返回值會在請求完成后傳入callback
selectAllComponents String selector, Function callback 使用選擇器選擇組件實(shí)例節(jié)點(diǎn),返回匹配到的全部組件實(shí)例對象組成的數(shù)組,此方法為異步方法,返回值會在請求完成后傳入callback
getRelationNodes String relationKey , Function callback 獲取這個關(guān)系所對應(yīng)的所有關(guān)聯(lián)節(jié)點(diǎn),參見 組件間關(guān)系。 此方法為異步方法,返回值會在請求完成后傳入callback 1.12.0.0

提示

selectComponent、selectAllComponents、getRelationNodes API 均為異步 API,需要指定回調(diào)函數(shù)。

提示

selectComponent、selectAllComponents 在 1.16.2.0 版本后支持同步寫法。開發(fā)者需要保證在調(diào)用API時,頁面已經(jīng)渲染完畢,比如頁面的onReady、setData的回調(diào)之后。

代碼示例

Component({
  behaviors: [],

  properties: {
    myProperty: {
      // 類型(必填),目前接受的類型包括:String, Number, Boolean, Object, Array, null(表示任意類型)
      type: String,

      // 屬性初始值(可選),如果未指定則會根據(jù)類型選擇一個
      value: '',

      // 屬性被改變時執(zhí)行的函數(shù)(可選),也可以寫成在 methods 段中定義的方法名字符串, 如:'_propertyChange'
      observer: function(newVal, oldVal, changedPath) {
         // 通常 newVal 就是新設(shè)置的數(shù)據(jù), oldVal 是舊數(shù)據(jù)
      }
    },
    myPropertyB: String // 簡化的定義方式
  },

  // 私有數(shù)據(jù),可用于模版渲染
  data: {},

  // 生命周期函數(shù),可以為函數(shù),或一個在 methods 段中定義的方法名
  attached: function () { 
    this.selectComponent('.class-name', function(res) {
      console.log(res)
    })

    // 
  },
  ready: function() { },

  // 組件自定義方法
  methods: {
    onMyButtonTap: function(){
      this.setData({ ... })
    },

    _myPrivateMethod: function(){
      // 這里將 data.A[0].B 設(shè)為 'test'
      this.setData({
        'A[0].B': 'test'
      })
    },

    _propertyChange: function(newVal, oldVal) {

    }
  }
})

注意:在 properties 定義中,屬性名采用駝峰寫法(propertyName);在 ttml/wxml 中,指定屬性值時則對應(yīng)使用連字符寫法(my-component property-name="value"),應(yīng)用于數(shù)據(jù)綁定時采用駝峰寫法(attr="")。

使用 Component 構(gòu)造器構(gòu)造頁面


小程序的頁面也可以視為自定義組件。因而,頁面也可以使用 Component 構(gòu)造器構(gòu)造,擁有與普通組件一樣的定義與實(shí)例方法。但此時要求對應(yīng) json 文件中包含 usingComponents 定義段。

此時,組件的屬性可以用于接收頁面的參數(shù),如訪問頁面 /pages/index/index?paramA=123&paramB=xyz ,如果聲明有屬性 paramA 或 paramB ,則它們會被賦值為 123 或 xyz 。

代碼示例

{
  "usingComponents": {}
}

在組件代碼中

Component({
  properties: {
    paramA: Number,
    paramB: String,
  },

  methods: {
    onLoad() {
      this.data.paramA // 頁面參數(shù) paramA 的值
      this.data.paramB // 頁面參數(shù) paramB 的值
    }
  }
})

注意:此功能正在逐步支持中,可能會存在不符合預(yù)期的問題


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號