SoFunction
Updated on 2025-04-05

Detailed explanation of @ and @ instances in Vue

1. @ (Stop the event from bubbles)

@: Prevent events from bubbles, that is, prevent click events from spreading from child components to parent components.

Case: We add a click event A to the parent element, and a click event B to the child element below it.

At this time, I want to click on the child element to trigger the click event of the child element, but in fact, the event of the child component will be triggered first, and then the event of the parent component will be triggered.

<div class='handle-box' @click="clickBox">
		<div @click="handleClick('Added')"><text>New</text></div>
		<div @click="handleClick('Revise')"><text>Revise</text></div>
		<div @click="handleClick('delete')"><text>delete</text></div>
</div>

Need to useStop events from bubbles(that is, prevent click events from continuing to spread) to solve this problem.

Change @click in the child component to @:

<div class='handle-box' @click="clickBox">
		<div @="handleClick('Added')"><text>New</text></div>
		<div @="handleClick('Revise')"><text>Revise</text></div>
		<div @="handleClick('delete')"><text>delete</text></div>
</div>

2. @ (the default behavior of blocking events)

@: The default behavior of blocking events. It will block the original event that triggers the dom, and only execute our custom events.

For example: Write a code<a>Tag, click<a>When tagging, a jump will be triggered by default, and it will jump to the target URL:

&lt;div&gt;
	&lt;a href="" rel="external nofollow"  rel="external nofollow"  target='_blank'&gt;Baidu&lt;/a&gt;
&lt;/div&gt;

But if we don't want it to jump, we click<a>When executing our custom method when tagging, you need to use @! The usage is as follows:

&lt;div&gt;
	&lt;a href="" rel="external nofollow"  rel="external nofollow"   target='_blank' @='handleClick('Revise')'&gt;Baidu&lt;/a&gt;
&lt;/div&gt;

Click again now<a>The tag will not jump to the target URL, but will execute our customized handleClick ('modify') event.

Attachment: Vue @ prevents events from being passed to ancestor elements (event bubbles)

No event triggers

&lt;template&gt;
  &lt;div @click="fun"&gt;
    &lt;div @&gt;
      &lt;!--sondiv--&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
export default {
  methods: {
    fun() {
      // Functions executed when clicking on the parent div      ('Clicked the parent div');
    },
  },
}
&lt;/script&gt;

Do not trigger parent event, trigger your own event

&lt;template&gt;
  &lt;div @click="doSomething"&gt;
    &lt;button @="doSomethingElse"&gt;The click event of the parent element will not be triggered&lt;/button&gt;
  &lt;/div&gt;
&lt;/template&gt;
&lt;script&gt;
export default {
  methods: {
    doSomething() {
      ('doSomething')
    },
    doSomethingElse() {
      ('doSomethingElse')
    }
  }
}
&lt;/script&gt;

Summarize

This is the end of this article about @ and @ in Vue. For more related @ and @ content in Vue, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!