- 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.
- 物聯(lián)網(wǎng)工程規(guī)劃技術(shù)
- 物聯(lián)網(wǎng)安全:理論、實(shí)踐與創(chuàng)新
- 物聯(lián)網(wǎng)檢驗(yàn)檢測(cè)技術(shù)
- OpenLayers Cookbook
- Spring Cloud微服務(wù)架構(gòu)進(jìn)階
- HTML5 Game development with ImpactJS
- Getting Started with WebRTC
- 互聯(lián)網(wǎng)安全的40個(gè)智慧洞見(jiàn):2014年中國(guó)互聯(lián)網(wǎng)安全大會(huì)文集
- 雷達(dá)饋線(xiàn)技術(shù)
- 城域網(wǎng)與廣域網(wǎng)(第2版)
- 圖解手機(jī)元器件維修技巧
- CCNP TSHOOT(642-832)認(rèn)證考試指南
- 新手易學(xué):新手學(xué)淘寶開(kāi)店
- 網(wǎng)管工具使用與技巧大全
- 網(wǎng)絡(luò)設(shè)計(jì)與應(yīng)用(第2版)