• Vue---组件的自定义事件和原生事件


    App.vue文件

    <template>

      <div class="mainbox">

       

       <button @click="change">点击button>

       

       <tbox v-on:myevent="fn">tbox>

       

       <box2 v-on:click.native="fn2">box2>

       

       <box3 v-on:click="fn3">box3>

      div>

    template>

    <script>

    import tbox from "./components/tbos.vue"

    import box2 from "./components/box2.vue"

    import box3 from "./components/box3.vue"

    export default {

      components:{

        tbox,

        box2,

        box3

     

     

    },

    methods:{

      //事件的三要素: 事件源 target  事件类型type  监听器handler

      change(){console.log("666666")},

      fn(){

        // tbox组件是时间源 myevent是tbox组件绑定的事件类型 ,fn是tbox绑定的监听器

        // fn要myevent触发了救护运行 myevent事件什么条件下才会触发?

        // myevent由tbox组件内部自己设计 什么时候触发

        console.log("fn函数调用")

      },

      fn2(){

        console.log("fn2函数调用")

      },

      fn3(){

        console.log("fn3函数调用")

      }

    }

    }

    script>

    <style>

    .mainbox{

      width: 300px;

      height: 300px;

      border: 1px solid black;

      margin: 0 auto;

    }

    style>

     

    tbos.vue文件

    <template>

        <div>

            <p>boxp>

            <button @click="x">boxbutton>

        div>

    template>

    <script>

    export default {

        data() {

            return {

            }

        },

        methods: {

            x(){

                this.$emit("myevent")//这个代码放在你想触发自定义事件的地方

            }

           

        }

    }

    script>

     

     

    bos2.vue文件

    <template>

        <div>

            <p>box2p>

            <button>box2button>

           

        div>

    template>

     

    bos3.vue文件

    <template>

     <div>

        <p>box3p>

        <button @click="x">box3button>

     div>

    template>

    <script>

    export default {

        methods: {

            x(){

                this.$emit("click")//这个代码放在你想触发自定义事件的地方

            }

           

        }

    }

    script>

     

  • 相关阅读:
    视频相似度检测算法软件,视频相似度检测算法图
    如何介绍自己的项目经验?
    运营商大数据怎样对目标客户群体进行精准定位?
    基于Labelstudio的UIE半监督智能标注方案(本地版)
    UVa 762 - We Ship Cheap
    理解RFC 3339时间格式和时区time zone
    pytest结合Allure生成测试报告
    小功能⭐️退出游戏 && 监听事件
    2024-02-23(Spark)
    C++核心编程(三十四)容器(set/multiset)
  • 原文地址:https://blog.csdn.net/cjx177187/article/details/126871439