I don't really understand the "this keyword" #10521
-
Hi, I've been wondering the exact use of the keyword "this" and when to use it or not in vue. I see that inside the template ("{{ data }}") is accessed without this., but inside script setup or export default methods and computed we use "this.variable", when to use what? Can we call the variables in all places without this.? Or there is any specific case that is MANDATORY to use this. to access to a variable or method? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
@FernandoGonzalezHomerti in template you use just the name of the variable/computed/method. Inside of the Can we call the variables in all places without this.? -- No. Calling method/variable inside of Or there is any specific case that is MANDATORY to use this. to access to a variable or method? -- Yes, use |
Beta Was this translation helpful? Give feedback.
-
https://stackoverflow.com/questions/43188317/use-this-in-vue-template |
Beta Was this translation helpful? Give feedback.
-
thanks! in vue 3 is the same way, right? im currently using vue 2, but i would like to know if this is also in vue 3 |
Beta Was this translation helpful? Give feedback.
@FernandoGonzalezHomerti in template you use just the name of the variable/computed/method. Inside of the
script
tag you usethis
keywordCan we call the variables in all places without this.? -- No. Calling method/variable inside of
script
tag would result in error. You can use variables/methods/computed inside of the template withoutthis
keywordOr there is any specific case that is MANDATORY to use this. to access to a variable or method? -- Yes, use
this
keyword inside ofscript
tag