본문 바로가기
study/Vue

[Vue] 28.computed : 함수를 데이터로 사용 (vueex6.html )

by 금이패런츠 2022. 5. 30.
728x90
반응형

vueex6.html

<!DOCTYPE html>
<!-- /vue1/src/main/webapp/20220530/vueex6.html -->
<html>
<head>
<meta charset="UTF-8">
<title>computed : 함수를 데이터로 사용</title>
<script type="text/javascript" src="https:unpkg.com/vue@2.5.16/dist/vue.js"></script>
</head>
<body>
<div id="example">
	<input type="text" v-model="num"><br>
	1부터 입력된 수까지의 합 : <span>{{sum}}</span>
</div>
<script type="text/javascript">
	let vSum = new Vue ({
		el : "#example",
		data : {num : 0},
		computed : { //계산형 속성
			sum : function(){
				//Number : 자바스크립트의 숫자형 객체
				//		this : Vue의 데이터. 문자열형이 기본임.
				let n = Number(this.num)
				//isNaN :  숫자가 아닌경우 True
				if(Number.isNaN(n) || n < 1) return 0;
				return ((1+n) * n) / 2; //숫자까지의 합 공식
			}
		}
	})
</script>
</body>
</html>
728x90
반응형