溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

微信小程序8種數(shù)據(jù)通信的方式小結(jié)

發(fā)布時間:2020-10-13 11:51:28 來源:腳本之家 閱讀:106 作者:WahFung 欄目:web開發(fā)

前言

數(shù)據(jù)通信在開發(fā)中是必不可少的一個環(huán)節(jié),也是我們必須掌握的知識。知道得越多的數(shù)據(jù)通信方式,實(shí)現(xiàn)業(yè)務(wù)會更加得心應(yīng)手。

下面我將這些通信方式歸類介紹:

  • 組件通信
  • 全局通信
  • 頁面通信

組件通信

properties

父組件向子組件通信,與 Vue 的 props 作用相同。

父組件向子組件傳數(shù)據(jù):

<my-component list="{{list}}"></my-component>

子組件接收數(shù)據(jù):

Component({
 properties:{
  list:{
   type: Array,
   value: []
  }
 },
 attached(){
  console.log(this.list)
 }
})

triggerEvent

子組件向父組件通信,與 Vue 的 $emit 作用相同
子組件觸發(fā)自定義事件:

Component({
 attached(){
  this.triggerEvent('customEvent',{ id: 10 })
 }
})

父組件接收自定事件:

<my-component list="{{list}}" bind:customEvent="customEvent"></my-component>
Page({
 customEvent(e){
  console.log(e.detail.id)
 }
})

selectComponent

使用選擇器選擇組件實(shí)例節(jié)點(diǎn),返回匹配到的第一個組件實(shí)例對象,類似 Vue 的 ref

<my-component id="mycomponent" list="{{list}}"></my-component>
Page({
 onLoad(){
  let mycomponent = this.selectComponent('#mycomponent')
  mycomponent.setData({
   list: []
  })
 }
})

selectOwnerComponent

選取當(dāng)前組件節(jié)點(diǎn)所在的組件實(shí)例(即組件的引用者),返回它的組件實(shí)例對象,類似 Vue 的 $parent

Component({
 attached(){
  let parent = this.selectOwnerComponent()
  console.log(parent)
 }
})

全局通信

globalData

將數(shù)據(jù)掛載到 app.js,這種方式在開發(fā)中很常用。通過getApp(),我們能夠在任何一個頁面內(nèi)訪問到app實(shí)例。
app.js

App({
 globalData:{
  list:[]
 }
})

page1.js

const app = getApp()
Page({
 onLoad(){
  app.globalData.list.push({
   id: 10
  })
 }
})

page2.js

const app = getApp()
Page({
 onLoad(){
  console.log(app.globalData.list) // [{id:10}]
 }
})

storage

storage并不是作為通信的主要方式。storage 主要是為了緩存數(shù)據(jù),并且最多只能存儲10M的數(shù)據(jù),我們應(yīng)該合理使用storage

wx.setStorageSync('timestamp', Date.now())
wx.getStorageSync('timestamp')
wx.removeStorageSync('timestamp')

eventBus

通過發(fā)布訂閱模式注冊事件和觸發(fā)事件進(jìn)行通信

簡單實(shí)現(xiàn)

class EventBus{
 constructor(){
  this.task = {}
 }

 on(name, cb){
  if(!this.task[name]){
   this.task[name] = []
  }
  typeof cb === 'function' && this.task[name].push(cb)
 }

 emit(name, ...arg){
  let taskQueen = this.task[name]
  if(taskQueen && taskQueen.length > 0){
   taskQueen.forEach(cb=>{
    cb(...arg)
   })
  }
 }

 off(name, cb){
  let taskQueen = this.task[name]
  if(taskQueen && taskQueen.length > 0){
   let index = taskQueen.indexOf(cb)
   index != -1 && taskQueen.splice(index, 1)
  }
 }

 once(name, cb){
  function callback(...arg){
   this.off(name, cb)
   cb(...arg)
  }
  typeof cb === 'function' && this.on(name, callback)
 }
}

export default EventBus

使用

app.js

import EventBus from '/util/EventBus'

wx.$bus = new EventBus()
page1.js
Page({
 onLoad(){
  wx.$bus.on('add', this.addHandler)
 },
 addHandler(a, b){
  console.log(a+b)
 }
})

page2.js

Page({
 onLoad(){
  wx.$bus.emit('add', 10, 20)
 },
})

頁面通信

getCurrentPages

getCurrentPages() 獲取當(dāng)前頁面棧,數(shù)組中第一個元素為首頁,最后一個元素為當(dāng)前頁面
元素為一個對象,里面存放著頁面的信息,包括route(頁面路徑)、options(onLoad拿到的參數(shù))、method、data等

Page({
 onLoad(){
  let pages = getCurrentPages()
  let lastPage = pages[pages.length-2]
  lastPage.setData({
   list: []
  })
 }
})

寫在最后

如果你還有其他的通信方式,歡迎交流~以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI