SoFunction
Updated on 2025-04-05

Distinguish between .capture and .self in vue and preliminary understanding

.capture and .self are distinguished

capture and self are mainly issues of function execution order

.capture first executes the parent function, and then executes the child trigger function (general usage).

That is, add a listener to the element. When the element bubbles, the element with the modifier will be triggered first. If there are multiple modifiers, they will be triggered from the outside to the inside.

<div v-on:='alert("1")' style="width: 100%;height: 45px;background-color: black;">
            <div v-on:click="alert('2')" style='width: 80%;margin-left: 10%;background-color: white;'>
                123
            </div>
        </div>

When clicking the child's div, alert('1') will be executed first, and then alert('2')

Self is a function that only executes the child itself

<div v-on:='alert("1")' style="width: 100%;height: 45px;background-color: black;">
            <div v-on:click="alert('2')" style='width: 80%;margin-left: 10%;background-color: white;'>
                123
            </div>
        </div>

At this time, clicking the child's div will execute alert('2'), and alert('1') will not be executed

Modifier capture and self

capture

.capture event modifier function uses event capture mode when adding event listeners

That is, add a listener to the element. When the element bubbles, the element with the modifier will be triggered first. If there are multiple modifiers, they will be triggered from the outside to the inside.

It means whoever has the event modifier will trigger it first. (Capture phase triggered, from outside to inside, without the capture modifier, bubbled from inside to outside)

&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;title&gt;.captureEvent modifier&lt;/title&gt;
    &lt;style type="text/css"&gt;
        * {
            margin: 0 auto;
            text-align: center;
            line-height: 40px;
        }
        div {
            width: 100px;
        }
        #obj1 {
            background: deeppink;
        }
        #obj2 {
            background: pink;
        }
        #obj3 {
            background: hotpink;
        }
        #obj4 {
            background: #ff4225;
        }
    &lt;/style&gt;
    &lt;script src="/vue/2.4.2/"&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div &gt;
    &lt;div  v-on:="doc(event)"&gt;
        obj1
        &lt;div  v-on:="doc(event)"&gt;
            obj2
            &lt;div  v-on:click="doc(event)"&gt;
                obj3
                &lt;div  v-on:click="doc(event)"&gt;
                    obj4
                    &lt;!--。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。Clickobj4When,The order of popup is:obj1、obj2、obj4、obj3;
                    because1,2Have modifiers,Therefore, the event is triggered first,Then it's4Trigger itself,The last bubbling event。
                    --&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;script type="text/javascript"&gt;
    var content = new Vue({
        el: "#content",
        data: {
            id: ''
        },
        methods: {
            doc(event) {
                 = ;
                alert()
            }
        }
    })
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

self

The callback is triggered only if the event is triggered from the element bound to the listener

&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;title&gt;.captureEvent modifier&lt;/title&gt;
    &lt;style type="text/css"&gt;
        * {
            margin: 0 auto;
            text-align: center;
            line-height: 40px;
        }
        div{
            width:200px;
        }
    &lt;/style&gt;
    &lt;script src="/vue/2.4.2/"&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div &gt;
   &lt;div class="box1"  style="background: red;" @="test1"&gt;
       box1
       &lt;div class="box2" style="background: yellow;"&gt;
           box2
&lt;!--box3Click to triggertest3Will not triggertest1,If removedselfModifier will trigger,That means addingselfElemental events,Callbacks will be executed only if they trigger it,Bubble events are not executed--&gt;
           &lt;div class="box3" style="background: pink;" @="test3"&gt;box3&lt;/div&gt;
       &lt;/div&gt;
   &lt;/div&gt;
&lt;/div&gt;
&lt;script type="text/javascript"&gt;
new Vue({
    el:'#app',
    data:{
    },
    methods:{
      test1(){
          ('box1');
      } ,
        test3(){
          ('box3');
        }
    }
})
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

The above is my temporary understanding. I hope it can help everyone. If you have different opinions, you can discuss and learn together! !