小程序--组件通信

一、父传子

与vue利用props类似,小程序是利用自定义属性:properties

javascript 复制代码
// components/my-nav/my-nav.js
Component({
  // 小程序组件默认样式是隔离,addGlobalClass设置为true可允许外部修改样式
  options: {
    addGlobalClass: true,
    // 只要使用到具名插槽,都需要将multipleSlots设置为true
    multipleSlots: true
  },
  externalClasses: ["custom-class"],
  // 内部数据
  data: {
    statusBarHeight: 0
  },
  // 外部数据--父传子
  properties: {
    // back: Boolean
    back: {
      type: Boolean,
      value: false
    },
    delta: {
      type: Number,
      value: 1
    }
  },
  // 生命周期
  lifetimes: {
    created(){},
    attached() {
      const { statusBarHeight } = wx.getSystemInfoSync()
      console.log(wx.getSystemInfoSync())
      console.log(statusBarHeight)
      this.setData({
        statusBarHeight: statusBarHeight
      })
    }
  }
})
html 复制代码
<view class="navigation-bar custom-class" style="padding-top: {{statusBarHeight}}px;">
  <view class="navigation-bar-title title-class">
    <text class="back-text" wx:if="{{ back }}" open-type="navigateBack" delta="{{ delta }}">返回</text>
    <slot name="left"></slot>自定义标/题<slot name="right"></slot>
  </view>
</view>
html 复制代码
<app-nav custom-class="nav_title" back="{{ true }}" >
  <text slot="left">◀</text>
  <text slot="right">▶</text>
</app-nav>

二、子传父

类似vue使用$emit方法,小程序利用triggerEvent和bind:自定义事件方法向父组件传递参数。

注意:小程序组件中的方法,要写在js的methods中

javascript 复制代码
// components/my-nav/my-nav.js
Component({
  // 小程序组件默认样式是隔离,addGlobalClass设置为true可允许外部修改样式
  options: {
    addGlobalClass: true,
    // 只要使用到具名插槽,都需要将multipleSlots设置为true
    multipleSlots: true
  },
  externalClasses: ["custom-class"],
  // 内部数据
  data: {
    statusBarHeight: 0
  },
  // 外部数据--父传子
  properties: {
    // back: Boolean
    back: {
      type: Boolean,
      value: false
    },
    delta: {
      type: Number,
      value: 1
    }
  },
  methods: {
    onTap() {
      // this.triggerEvent('自定义事件',参数)
      this.triggerEvent('getBarHeight',this.data.statusBarHeight)
    }
  },
  // 生命周期
  lifetimes: {
    created(){},
    attached() {
      const { statusBarHeight } = wx.getSystemInfoSync()
      console.log(wx.getSystemInfoSync())
      console.log(statusBarHeight)
      this.setData({
        statusBarHeight: statusBarHeight
      })
    }
  }
})
javascript 复制代码
<view class="navigation-bar custom-class" style="padding-top: {{statusBarHeight}}px;">
  <view class="navigation-bar-title title-class">
    <text class="back-text" wx:if="{{ back }}" open-type="navigateBack" delta="{{ delta }}">返回</text>
    <slot name="left" bind:tap="onTap"></slot>自定义标题<slot name="right"></slot>
  </view>
</view>
html 复制代码
<!-- 页面注册自定义组件 -->
<!-- <page-search /> -->
<app-nav custom-class="nav_title" back="{{ true }}" bind:getBarHeight="getBarHeightFn" >
  <text slot="left">◀</text>
  <text slot="right">▶</text>
</app-nav>
javascript 复制代码
// pages/component/index.js
Page({
  getBarHeightFn(e) {
    console.log(e.detail)
  }
})
相关推荐
weifont2 小时前
聊一聊Electron中Chromium多进程架构
javascript·架构·electron
大得3692 小时前
electron结合vue,直接访问静态文件如何跳转访问路径
javascript·vue.js·electron
水银嘻嘻4 小时前
12 web 自动化之基于关键字+数据驱动-反射自动化框架搭建
运维·前端·自动化
it_remember4 小时前
新建一个reactnative 0.72.0的项目
javascript·react native·react.js
小嘟嚷ovo4 小时前
h5,原生html,echarts关系网实现
前端·html·echarts
十一吖i5 小时前
Vue3项目使用ElDrawer后select方法不生效
前端
只可远观5 小时前
Flutter目录结构介绍、入口、Widget、Center组件、Text组件、MaterialApp组件、Scaffold组件
前端·flutter
周胡杰5 小时前
组件导航 (HMRouter)+flutter项目搭建-混合开发+分栏效果
前端·flutter·华为·harmonyos·鸿蒙·鸿蒙系统
敲代码的小吉米5 小时前
前端上传el-upload、原生input本地文件pdf格式(纯前端预览本地文件不走后端接口)
前端·javascript·pdf·状态模式
是千千千熠啊5 小时前
vue使用Fabric和pdfjs完成合同签章及批注
前端·vue.js