Vue - Methods, Computed 속성의 차이

Doyoon Lee·2020년 9월 20일
0
post-thumbnail

methods

methods 는 호출될때만 실행되는 함수들이다.

(리액트에서 함수를 arrow function으로 정의내렸던 event를 받는 handler 함수들과 유사하다.)

computed

computed 는 대상 데이터 (state)의 값이 변경되면 자동으로 실행된다.

watch

watch 속성은 데이터 변화를 감지하여 자동으로 특정 로직을 수행한다.

computed 와 watch 속성은 특정 로직(함수)를 수행해서 상태값을 가져다가 적용하는 점이 비슷한데,

computed 속성은 내장 API 를 활용한 간단한 연산에 적합하고,

watch 는 데이터 호출과 같이 시간이 더 많이 소요되는 비동기 처리에 적합하다.


computed: {
	attachRed: function() {
		return {
		active: this.isActive && !this.error,
		'text-error': this.error && this.error.type === 'fatal' };
	 }
},

0개의 댓글