[Vue.js] Vue.js 시작하기 - 2 [ 조건문과 반복문 ]

리트리버J

·

2020. 12. 29. 01:00

728x90

1. v-if 조건문

v-if="boolean값"을 통하여,

태그를 보이기 / 숨김 처리를 할 수 있다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
    <title>Document</title>
</head>
<body>
    <div id="app-3">
        <p v-if="seen">이제 나를 볼 수 있어요</p>
    </div>
    <script>
        var app3 = new Vue({
        el: '#app-3',
        data: {
            seen: true
        }
        })
    </script>  
</body>
</html>
cs

app3의 seen이 true이기 때문에 정상적으로 출력된다.
false값을 할당하니 사라졌다!! 

2. v-for 반복문

v-for="조건"을 통하여 반복하여 태그를 생성 할 수 있다.

객체 배열 todos의 인덱스 값 하나하나가 todo로 들어간다.

todo.key를 통하여 value를 불러온다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
    <title>Document</title>
</head>
<body>
    <div id="app-4">
        <ol>
          <li v-for="todo in todos">
            {{ todo.text }}
          </li>
        </ol>
    </div>
    <script>
        var app4 = new Vue({
        el: '#app-4',
        data: {
            todos: [
            { text: 'JavaScript 배우기' },
            { text: 'Vue 배우기' },
            { text: '무언가 멋진 것을 만들기' }
            ]
        }
        })
    </script>  
</body>
</html>
cs

<li>태그 3개가 생성되었다.
Vue 인스턴스 app4, 배열 todos 이므로 JS 배열의 push함수를 사용 할 수 있다!!

 

728x90