美文网首页
vue父-子-点击事件传递

vue父-子-点击事件传递

作者: O蚂蚁O | 来源:发表于2020-04-11 11:10 被阅读0次

    1、

    $emit
    

    2、
    父组件

    <template>
      <section class="product-entering-list">
        <child
          :options="options"
        ></child>
      </section>
    </template>
    
    <script>
    import child from "./child"
    export default {
      name: 'test',
      components: { child },
      data() {
        return {
          options: [
            {
              txt: '点击我',
              handler: () => {
                this.handleClick()
              }
            }
          ]
        }
      },
      mounted() {
        
      },
      methods: {
        handleClick() {
          console.log('我是父组件');
        }
      }
    }
    </script>
    
    <style lang="less">
    
    </style>
    

    子组件

    <!--
     * @Author: your name
     * @Date: 2020-04-11 10:37:24
     * @LastEditTime: 2020-04-11 10:57:50
     * @LastEditors: your name
     * @Description: In User Settings Edit
     * @FilePath: \longzhu-mdm-web\src\views\kit\productLibrary\productScan\child.vue
     -->
    <template>
      <section class="product-entering-list">
        <el-button v-for="(item, index) in options" :key="index" 
          type="primary"
          @click.stop="() => item.handler()"
        >
          {{item.txt}}
        </el-button>
      </section>
    </template>
    
    <script>
    
    export default {
      name: 'child',
      components: { },
      props: {
        options: { 
          type: Array,
          default: () => ([])
        },
      },
      data() {
        return {
        }
      },
      mounted() {
        
      },
      methods: {
        handleClick(val) {
          
        }
      }
    }
    </script>
    
    <style lang="less">
    
    </style>
    

    相关文章

      网友评论

          本文标题:vue父-子-点击事件传递

          本文链接:https://www.haomeiwen.com/subject/tdzemhtx.html