小程序学习之旅----slot 子组件调用父组件的方法、父组件调用子组件的方法

slot子组件

<!--pages/user/user.wxml-->
<header title='{{title}}'></header>
{{title}}
<footer>
  <button>我是footer子组件里的按钮</button>
</footer>

在footer子组件里slot调用

<!--components/footer/footer.wxml-->
<view class="footer" hover-class="none" hover-stop-propagation="false">
  footer组件
</view>
<slot></slot>

**父组件调用子组件的方法    
    1、调用子组件的时候定义一个id       <header id="header"></header>

<!--pages/user/user.wxml-->
<!-- 此处定义一个id是为了在父组件中执行子组件的方法用 -->
<header title='{{title}}' id='header'></header>
{{title}}
<footer>
  <button>我是footer子组件里的按钮</button>
</footer>
<br />
<button bindtap='getChildRun'>获取子组件的ChildRun方法</button>

    2、父组件获取子组件的实例          var header = this.selectComponent("#header")

// pages/user/user.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: '用户组件title'
  },
  getChildRun () {
    var header = this.selectComponent('#header')
    // 父组件里执行子组件的方法
    header.childRun()
    // 获取子组件中data数据
    console.log(header.data.msg)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  }
})


    3、header.方法名                 调用子组件里面的方法                 header.data.msg父组件里面直接获取子组件的数据

// components/header/header.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    // title: String
    // 如果没传值,就让显示默认值
    title: {
      type: "String",
      value: "头部"
    }
  },

  /**
   * 组件的初始数据,绑定的属性不要与properties里重复
   */
  data: {
    msg: '获取组件data值'
  },

  /**
   * 组件的方法列表
   */
  methods: {
    run () {
      console.log('run')
      console.log(this.data.msg);
      // 重新设定msg的值
      this.setData({
        msg: '改变后的值'
      })
    },
    childRun () {
      console.log('我是子组件的childRun方法')
    }
  }
})


**子组件执行父组件里面的方法

<!--components/footer/footer.wxml-->
<view class="footer" hover-class="none" hover-stop-propagation="false">
  footer组件
</view>
<slot></slot>
<button bindtap='getfather'>触发父组件的run方法</button>
// components/footer/footer.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    getfather () {
      this.triggerEvent('parent', '子组件的数据')
    }
  }
})


    1、子组件给父组件传值

        this.triggerEvent('parent', 数据, 参数)

<!--pages/user/user.wxml-->
<!-- 此处定义一个id是为了在父组件中执行子组件的方法用 -->
<header title='{{title}}' id='header'></header>
{{title}}
<!-- <footer>
  <button>我是footer子组件里的按钮</button>
</footer> -->
<footer bindparent='parentrun'></footer>
<br />
<button bindtap='getChildRun'>获取子组件的ChildRun方法</button>

父组件中绑定parent,执行parentrun方法,parent事件是前面子传父定义的parent,可以任意起名,但是bind+任意名时,要一致。
    
    2、    <footer bindparent="parentrun" />

// pages/user/user.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: '用户组件title'
  },
  getChildRun () {
    var header = this.selectComponent('#header')
    // 父组件里执行子组件的方法
    header.childRun()
    // 获取子组件中data数据
    console.log(header.data.msg)
  },
  parentrun (e) {
    console.log(e.detail)
    console.log('我是父组件的run方法')
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  }
})

通过e.detaill可以拿到子组件传过来的值

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在 Vue 中,组件可以通过使用 `<slot>` 元素在组件中创建插槽,并在组件调用组件的插槽。下面是一个简单的示例: 组件: ``` <template> <div> <slot name="header"></slot> <div>组件内容</div> <slot name="footer"></slot> </div> </template> ``` 组件: ``` <template> <div> <child-child-component> <template v-slot:header> <h1>这是组件的头部</h1> </template> <template v-slot:footer> <p>这是组件的底部</p> </template> </child-child-component> </div> </template> <script> import ChildChildComponent from './ChildChildComponent.vue' export default { components: { ChildChildComponent } } </script> ``` 组件: ``` <template> <div> <child-component> <template v-slot:header> <h1>这是组件的头部</h1> </template> <child-child-component> <template v-slot:header> <h1>这是组件的头部</h1> </template> <template v-slot:footer> <p>这是组件的底部</p> </template> </child-child-component> <template v-slot:footer> <p>这是组件的底部</p> </template> </child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue' export default { components: { ChildComponent } } </script> ``` 在上面的示例中,组件调用组件的两个插槽,分别是名为 header 和 footer 的插槽。组件中又调用组件,并在组件中定义了名为 header 和 footer 的插槽。在组件中,使用 `<child-child-component>` 元素来调用组件,并在其中使用 `<template>` 元素来为组件的插槽提供内容,并使用 `v-slot` 指令来指定插槽的名称。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瑞朋哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值