[Vue 03] computed(를 보다가 vue 반응형으로 새버린 글)
예
예준천
const a= ref(3);
let b = a.value; // 반응성이 깨짐
const c = a;
const d = computed(() => a.value);function whenDepsChange(update) {
const effect = () => {
activeEffect = effect
update()
activeEffect = null
}
effect()
}import {ref, reactive,computed } from 'vue'
const obj = reactive({name:'천에준', attr:{age:21, height: 181}});
const myComputed = computed(() => obj);
obj.attr.age++;
console.log(myComputed.value);import { reactive,computed } from 'vue'
const obj = reactive({});
const myComputed = computed(() => {
return obj.newProp; // 동적으로 추가된 속성에 대한 변화를 추적 가능
});
obj.newProp = 'new property';
console.log(myComputed.value); // 'new property'import {ref, reactive,computed } from 'vue'
const obj = reactive([]);
const myComputed = computed(() => {
return obj.length; // 동적으로 추가된 속성에 대한 변화를 추적 가능
//return obj.x();
});
obj.push(3,4,5);
console.log(myComputed.value);import { ref,reactive,computed } from 'vue'
const obj = ref([1,2,3]);
const myComputed = computed(() => {
return obj.value; // 동적으로 추가된 속성에 대한 변화를 추적 가능
});
//obj.value[0] = 10;
obj.value = [10,...obj.value.slice(1)]
console.log(myComputed.value); // 'new property'const org = ref(3);
// computed
const a = computed(() => org.value * 2);
// 메소드
const doubleValue = (x) => x*2 ;
<p> {{doubleValue(org)}}</p><script setup>
import { ref, computed } from 'vue'
const firstName = ref('John')
const lastName = ref('Doe')
const fullName = computed({
// getter
get() {
return firstName.value + ' ' + lastName.value
},
// setter
set(newValue) {
// 참고: 분해 할당 문법을 사용함.
[firstName.value, lastName.value] = newValue.split(' ')
}
})
</script>