小程序子组件调用父组件方法、父组件调用子组件方法

发布于:2024-12-20 ⋅ 阅读:(10) ⋅ 点赞:(0)

1、子组件调用父组件方法

     子组件this.triggerEvent('finish');

    startShare(e) {
      let url = config.apiUrl + "/business/lzShare/edit";
      let data = this.data.currentData;
      util.httpPut(url, data).then((res) => {
        this.triggerEvent('finish');
        console.log(res.result);
      })
 
    },

父组件监听

bind:finish="finishShare"

    <view class="padding flex flex-col flex-space-between " >
      <share bind:finish="finishShare"  data="{{shareData}}"  style="margin-top:40px"></share>
    </view>

2、父组件调用子组件方法

父组件index.json引用子组件

 父组件引用子组件,子组件设置id值 

调用

this.selectComponent('#filedir')

    saveMoveData(e) {
      this.filedir = this.selectComponent('#filedir')
      let url = config.apiUrl + "/business/lzDocs/changeDir";
      let param = {id: this.data.selectData.id, parentId: this.filedir.getSelect().id}
      util.httpPost(url, param).then((res) => {
         this.loadData();
         this.setData({showMove : false})
      })
    },

子组件的方法

// components/filedir/filedir.js
const icon = require('../../utils/icon.js');

Component({

  /**
   * 组件的属性列表
   */
  properties: {
    data:{}
  },
  observers: {
    'data': function(newVal, oldVal) {
      console.log("newVal====", newVal)
      this.setData({currentData: newVal, dirData: newVal})
      this.setData({navData: []})
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    navData:[],
    currentData:[],
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getSelect() {
      if (this.data.navData.length == 0) {
        return null;
      } else {
        return this.data.navData[this.data.navData.length-1];
      }
    },
     

  }
})


网站公告

今日签到

点亮在社区的每一天
去签到