Home  >  Article  >  Web Front-end  >  Who does this in vue point to?

Who does this in vue point to?

下次还敢
下次还敢Original
2024-05-02 22:33:35405browse

In Vue, the point of this depends on its context: In the root instance: Points to the Vue instance itself. Inside the method: Points to the Vue instance to which the method belongs. Inside the computed property: Points to the Vue instance event handler function to which the getter function belongs. Internal: Points to the DOM element that triggered the event. Internal template: Points to the Vue instance to which the getter function belongs.

Who does this in vue point to?

this in Vue points to

In Vue, the this keyword points to different objects in different places, depending on its context.

Root instance

In the Vue root instance, this points to the Vue instance itself. This means that you can access all properties and methods of the Vue instance through this.

Inside the method

Inside the Vue method, this points to the Vue instance to which the method belongs. This means you can access all properties and methods of the instance via this.

Inside a computed property

Inside a computed property, this points to the Vue instance to which the getter function belongs. This means you can access all properties and methods of the instance via this.

Inside the event handler function

Inside the event handler function, this points to the DOM element that triggered the event. This means you can access the properties and methods of that DOM element via this.

Inside the template

Inside the template, this points to the Vue instance to which the getter function belongs. This means you can access all properties and methods of the instance via this.

It should be noted that:

  • In the arrow function of the JavaScript function, this points to in the parent scope this, not this inside the function.
  • When passing the event handler function through the bind or v-on modifier, this needs to be explicitly bound, otherwise this will point to the DOM element.

The above is the detailed content of Who does this in vue point to?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn