- html
<!DOCTYPE html> <html lang="ko"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Vue Test</title> <script src="vue.js"></script> </head> <body> <div id="app"> {{msg}} </div> <script> new Vue( { el: "#app", data: {msg : 'Hello Vue!!!'} } ) </script> </body> </html>

<script src="url">과 같은 태그로 호출해서 사용하면 된다. 
- html
<!DOCTYPE html> <html lang="ko"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Vue Test</title> <script src="https://cdn.jsdelivr.net/npm/vue@2.7.10/dist/vue.js"></script> </head> <body> <div id="app2"> <span v-bind:title="msg"> span안에 있는 내용1 </span> </div> <script> new Vue( { el: "#app2", data: {msg : '이 message는 ' + new Date() + '에 작성되었스니다.'} } ) </script> </body> </html>
- html
<div id="app"> <div class="text" v-bind:class="{active : active}"> {{msg}} </div> </div>
- css
.text { font-size: 70px } .text.active{ color: red, }
- 만약에 text의 active가 붙어있다면 color를 red로 바꾸겠다
- javascript
new Vue({ el: '#app', data: { msg: 'Hello vue', active : true } })
- html
<div id="app"> <p v-if="seen"> 나를 보세요~! </p> </div>
- javascript
new Vue({ el: '#app', data: { msg: 'Hello vue', seen : true } })
<div id="app">
<div v-if="msg">{{msg}} ~~</div>
</div>
- html
<div id="app"> <ol> <li v-for="item in items" v-bind:key="item.id"> {{item.msg}} </li> </ol> </div>
- javascript
new Vue({ el: '#app', data: { items: [ { id: '1', msg: 'hello world' }, { id: '2', msg: 'hi' }, { id: '3', msg: 'good' } ] } })
- v-for에는 "v-bind:key="item.id"" 정의가 필요하다
v-on : vue instacne의 method 호출하는 디렉티브
- html
<div id="app5"> <p>{{msg}}</p> <button v-on:click = "reverse">버튼을</button> </div>
- javascript
new Vue({ el: '#app5', data: { msg : "hi~" }, methods: { reverse: function () { this.msg = this.msg.split('').reverse().join('') } } })
- html
<div id="app5"> <p> {{msg}} </p> <input v-model="msg"> </div>
- javascript
new Vue({ el: '#app5', data: { msg : 'hi~' } })
- html
<div id="app5"> <div class = "css1" v-bind:class="{active : tg}"> </div> <button v-on:click="tgElement">버튼1</button> </div>
- javascript
new Vue({ el: '#app5', data: { tg : true }, methods: { tgElement: function(){ this.tg = !this.tg } } })
- css
.css1 { width : 150px; height: 150px; background: black; border-radius: 10px; cursor: pointer; transition: 1s; } .css1.active{ width : 10px; height: 10px; background: blue; }
- html
<div id="app5"> <ol> <todo-item v-for="item in list2" v-bind:todo="item" v-bind-key="item.id"> </todo-item> </ol> </div>
- javacript
Vue.component('todo-item', { props: ['todo'], template : '<li>{{todo.text}}</li>' }) new Vue({ el: '#app5', data: { list2: [ { id: 0, text: 'tomato' }, { id: 1, text: 'banana' }, { id: 2, text: 'goguma' } ] } })

- html
<div id="app"> <ul> <li v-for="item in todos" v-bind:key="item.id"> <input type="checkbox" v-model="item.done"> <span v-bind:class="{done: item.done}">{{item.text}}</span> </li> </ul> </div>
- javascript
new Vue({ el: '#app', data: { test : { text: 'sss', done: true }, todos: [ { id: '1', text: '아침 먹기', done: true }, { id: '2', text: '점심 먹기', done: false }, { id: '3', text: '저녁 먹기', done: false }, { id: '4', text: '야식 먹기', done: false }, { id: '5', text: '간식 먹기', done: false } ] } })
- html
<div id="app"> <ul> <my-cp1 v-for="todo in todos" v-bind:key="todo.id" v-bind:item="todo"> </my-cp1> </ul> </div>
- javascript
Vue.component('my-cp1',{ props: ['item'], template: `<li> <input type="checkbox" v-model="item.done"> <span v-bind:class="{done: item.done}">{{item.text}}</span> </li>` }) new Vue({ el: '#app', data: { todos: [ { id: '1', text: '아침 먹기', done: true }, { id: '2', text: '점심 먹기', done: false }, { id: '3', text: '저녁 먹기', done: false }, { id: '4', text: '야식 먹기', done: false }, { id: '5', text: '간식 먹기', done: false } ] } })
- css
- li안에 있는 span에게만 적용하겠다는 뜻이다.
li span.done { text-decoration: line-through; }
- 모든 span에 적용하겠다는 뜻이다
span.done { text-decoration: line-through; }
출처
https://effectivecode.tistory.com/1205
https://velog.io/@hikoand/Vue.js-Vue.js-%EC%9E%85%EB%AC%B82-v-for-v-bind