- Computed Properties: Use when you want to calculate or derive a value based on existing data. They are synchronous and return a value.
- Watchers: Use when you need to perform asynchronous or complex operations in response to changes in data. They allow you to react to changes but don't return a value directly.
<template>
<div>
<p>{{ fullName }}</p>
</div>
</template>
<script>
export default {
data() {
return {
firstName: 'John',
lastName: 'Doe'
};
},
computed: {
fullName() {
return `${this.firstName} ${this.lastName}`;
}
}
};
</script>
<template>
<div>
<p>{{ message }}</p>
</div>
</template>
<script>
export default {
data() {
return {
count: 0,
message: ''
};
},
watch: {
count(newValue, oldValue) {
this.message = `Count changed from ${oldValue} to ${newValue}`;
}
}
};
</script>
한글로 하면 리턴이 있냐 없냐의 차이인가? 그리고 watch는 old나 new가 있고? 둘다 지정한 값이 변하면 내부 function이 작동한다.
'🖥️FE🖥️ > 📗Vue📗' 카테고리의 다른 글
Shallow Copy Deep Copy (1) | 2023.11.26 |
---|---|
Vue Emit 간단한 실습 코드 (0) | 2023.11.23 |
[VUE]TypeError: Cannot read properties of null (reading 'data') (1) | 2023.11.21 |
vue-router (0) | 2023.10.30 |
Vue (0) | 2023.10.24 |