- Vue.js 2 Design Patterns and Best Practices
- Paul Halliday
- 189字
- 2021-06-24 18:33:09
How Vue handles 'this'
You may have noticed up to this point that we're able to reference values inside of our data, methods, and other objects using this syntax, but the actual structure of our instance is this.data.propertyName or this.methods.methodName; all of this is possible due to the way Vue proxies our instance properties.
Let's take a very simple Vue application that has one instance. We have a data object that has a message variable and a method named showAlert; how does Vue know how to access our alert text with this.message?
<template>
<button @click="showAlert">
Show Alert</button>
</template>
<script>
export default {
data() {
return {
message: 'Hello World!',
};
},
methods: {
showAlert() {
alert(this.message);
},
},
};
</script>
Vue proxies the instance properties to the top level object, allowing us to access these properties via this. If we were to log out the instance to the console (with the help of Vue.js devtools), we'd get the following result:

The key properties to look at within the preceding screenshot are message and showAlert, both of which are defined on our Vue instance yet proxied to the root object instance at initialization time.
- C++黑客編程揭秘與防范
- Building E-commerce Sites with VirtueMart Cookbook
- Drush User’s Guide
- Working with Legacy Systems
- 基于性能的保障理論與方法
- 網絡環境中基于用戶視角的信息質量評價研究
- 網絡設計與應用(第2版)
- 網管工具使用與技巧大全
- 6G無線網絡空口關鍵技術
- Learning Storm
- Getting Started with Memcached
- Guide to NoSQL with Azure Cosmos DB
- 現場綜合化網絡運營與維護:運營商數字化轉型技術與實踐
- Hands-On Reactive Programming in Spring 5
- 智能物聯安防視頻技術基礎與應用