- Vue.js 2 Design Patterns and Best Practices
- Paul Halliday
- 360字
- 2021-06-24 18:33:10
Data properties
When we add a variable to our data object, we're essentially creating a reactive property that updates the view any time it changes. This means that, if we had a data object with a property named firstName, that property would be re-rendered on the screen each time the value changes:
<!DOCTYPE html>
<html>
<head>
<title>Vue Data</title>
<script src="https://unpkg.com/vue"></script>
</head>
<body>
<div id="app">
<h1>Name: {{ firstName }}</h1>
<input type="text" v-model="firstName">
</div>
<script>
const app = new Vue({
el: '#app',
data: {
firstName: 'Paul'
}
});
</script>
</body>
</html>
This reactivity does not extend to objects added to our Vue instance after the instance has been created outside of the data object. If we had another example of this, but this time including appending another property such as fullName to the instance itself:
<body>
<div id="app">
<h1>Name: {{ firstName }}</h1>
<h1>Name: {{ name }}</h1>
<input type="text" v-model="firstName">
</div>
<script>
const app = new Vue({
el: '#app',
data: {
firstName: 'Paul'
}
});
app.fullName = 'Paul Halliday';
</script>
</body>
Even though this item is on the root instance (the same as our firstName variable), fullName is not reactive and will not re-render upon any changes. This does not work because, when the Vue instance is initialized, it maps over each one of the properties and adds a getter and setter to each data property, thus, if we add a new property after initialization, it lacks this and is not reactive.
How does Vue achieve reactive properties? Currently, it uses Object.defineProperty to define a custom getter/setter for items inside of the instance. Let's create our own property on an object with standard get/set features:
const user = {};
let fullName = 'Paul Halliday';
Object.defineProperty(user, 'fullName', {
configurable: true,
enumerable: true,
get() {
return fullName;
},
set(v) {
fullName = v;
}
});
console.log(user.fullName); // > Paul Halliday
user.fullName = "John Doe";
console.log(user.fullName); // > John Doe
As the watchers are set with a custom property setter/getter, merely adding a property to the instance after initialization doesn't allow for reactivity. This is likely to change within Vue 3 as it will be using the newer ES2015+ Proxy API (but potentially lacking support for older browsers).
There's more to our Vue instance than a data property! Let's use computed to create reactive, derived values based on items inside of our data model.
- MERN Quick Start Guide
- SOA用戶指南
- 物聯(lián)網(wǎng)(IoT)基礎(chǔ):網(wǎng)絡(luò)技術(shù)+協(xié)議+用例
- 異構(gòu)基因共表達(dá)網(wǎng)絡(luò)的分析方法
- TCP/IP入門經(jīng)典(第5版)
- 通信簡史:從信鴿到6G+
- Getting Started with WebRTC
- 網(wǎng)絡(luò)安全應(yīng)急響應(yīng)技術(shù)實(shí)戰(zhàn)
- Master Apache JMeter:From Load Testing to DevOps
- 網(wǎng)管第一課:網(wǎng)絡(luò)操作系統(tǒng)與配置管理
- Microsoft Power Platform Enterprise Architecture
- 物聯(lián)網(wǎng)的機(jī)遇與利用
- 物聯(lián)網(wǎng)基礎(chǔ)及應(yīng)用
- 現(xiàn)場綜合化網(wǎng)絡(luò)運(yùn)營與維護(hù):運(yùn)營商數(shù)字化轉(zhuǎn)型技術(shù)與實(shí)踐
- OSPF協(xié)議原理與功能拓展