<div id="app">
<p>메시지: {{ msg }}</p>
<p>HTML 메시지 : {{ rawHTML }}</p>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
msg: 'Text interpolation',
rawHTML: '<span style="color:red">빨간글씨</span>'
}
})
</script>
v-html
directive을 사용하여 data와 바인딩<div id="app">
<p>메시지: {{ msg }}</p>
<span v-html="rawHTML"></span>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
msg: 'RAW HTML',
rawHTML: '<span style="color:red">빨간글씨</span>'
}
})
</script>
<div id="app">
<p>메시지: {{ msg }}</p>
<p>{{ msg.split('').reverse().join('') }}</p>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
msg: 'Text interpolation',
}
})
</script>
:
을 통해 전달인자를 받을 수 있음.
으로 표시되는 특수 접미사 – directive를 특별한 방법으로 바인딩 해야 함<div id="app">
...
</div>
<div id="app2">
</div>
<!-- Vue CDN -->
<script>
...
const app2 = new Vue({
el: '#app2',
})
</script>
<div id="app">
<p v-text="message"></p>
<!-- 같음 -->
<p>{{ message }}</p>
</div>
const app = new Vue({
el: '#app',
data: {
message: 'Hello Vue!',
}
})
<div id="app">
<p v-html="html"></p>
</div>
<!-- Vue CDN -->
<script>
const app = new Vue({
el: '#app',
data: {
html: '<a href="https://www.google.com">GOOGLE</a>'
}
})
</script>
<div id="app">
<p v-show="isActive">보이니? 안보이니?</p>
</div>
<!-- Vue CDN -->
<script>
const app = new Vue({
el: '#app',
data: {
isActive: false
}
})
</script>
<div id="app">
<p v-if="isActive">안보이니? 보이니?</p>
</div>
<!-- Vue CDN -->
<script>
const app = new Vue({
el: '#app',
data: {
isActive: false
}
})
</script>
for .. in ..
형식으로 작성(char, index)
형태로 사용 가능<div id="app">
<h2>String</h2>
<div v-for="char in myStr">
{{ char }}
</div>
<div v-for="(char, index) in myStr" :key="index">
<p>{{ index }}번째 문자열 {{ char }}</p>
</div>
</div>
<!-- Vue CDN -->
<script>
const app = new Vue({
el: '#app',
data: {
// 1. String
myStr: 'Hello!',
}
})
</script>
dot notation
으로 접근 할 수 있음<div id="app">
<h2>Array</h2>
<div v-for="(item, index) in myArr" :key="`ssafy-${index}`">
<p>{{ index }}번째 아이템 {{ item }}</p>
</div>
<div v-for="(item, index) in myArr2" :key="`arry-${index}`">
<p>{{ index }}번째 아이템</p>
<p>{{ item.name }}</p>
</div>
</div>
<!-- Vue CDN -->
<script>
const app = new Vue({
el: '#app',
data: {
// Array
myArr: ['python', 'django', 'vue.js'],
// Array with Object
myArr2: [
{ id: 1, name: 'python', completed: true},
{ id: 2, name: 'django', completed: true},
{ id: 3, name: 'vue.js', completed: false},
],
}
})
</script>
<div id="app">
<h2>Object</h2>
<div v-for="value in myObj">
<p>{{ value }}</p>
</div>
<div v-for="(value, key) in myObj" :key="key">
<p>{{ key }} : {{ value }}</p>
</div>
</div>
<!-- Vue CDN -->
<script>
const app = new Vue({
el: '#app',
data: {
// Object
myObj: {
name: 'harry',
age: 27
},
}
})
</script>
<div v-for="(item, index) in myArr2" :key="`arry-${index}`">
<p>{{ index.id }}번째 아이템</p>
<p>{{ item.name }}</p>
</div>
:
을 통해 전달받은 인자를 확인<div id="app">
<button v-on:click="number++">increase Number</button>
<p>{{ number }}</p>
</div>
<!-- Vue CDN -->
<script>
const app = new Vue({
el: '#app',
data: {
number: 0,
},
})
</script>
:
을 통해 전달된 인자에 따라 특별한 modifiers (수식어)가 있을 수 있음@
shortcut 제공<div id="app">
<button v-on:click="toggleActive">toggle isActive</button>
<p>{{ isActive }}</p>
<button @click="checkActive(isActive)">check isActive</button>
</div>
<!-- Vue CDN -->
<script>
const app = new Vue({
el: '#app',
data: {
number: 0,
isActive: false,
},
methods: {
toggleActive: function () {
this.isActive = !this.isActive
},
checkActive: function (check) {
console.log(check)
}
}
})
</script>
:
shortcut 제공<style>
.red-text {
color: red;
}
.border-black {
border: solid 1px black;
}
</style>
<div id="app">
<a v-bind:href="url">Go To GOOGLE</a>
<!-- v-bind는 생략가능 -->
<p :class="redTextClass">빨간글씨</p>
<!-- 조건부 바인딩 -->
<p :class="{ 'red-text': true }">빨간글씨</p>
<!-- 다중 바인딩 -->
<p :class="[redTextClass, borderBlack]">빨간글씨, 테두리</p>
</div>
<!-- Vue CDN -->
<script>
const app = new Vue({
el :'#app',
data: {
url: 'https://www.google.com',
redTextClass: 'red-text',
borderBlack: 'border-black',
},
})
</script>
<style>
.dark-mode {
color: white;
background-color: black;
}
.white-mode {
color: black;
background-color: white;
}
</style>
<div id="app">
<p :class="theme">모드 전환</p>
<button @click="darkModeToggle">{{ theme }} 모드 토글</button>
</div>
<!-- Vue CDN -->
<script>
const app = new Vue({
el :'#app',
data: {
isAcitve: true,
theme: 'dark-mode',
},
methods: {
darkModeToggle() {
this.isAcitve = !this.isAcitve
if (this.isAcitve) {
this.theme = 'dark-mode'
} else {
this.theme = 'white-mode'
}
}
}
})
</script>
<div id="app">
<h3>{{ myMessage }}</h3>
<input v-model="myMessage" type="text">
<hr>
</div>
<!-- Vue CDN -->
<script>
const app = new Vue({
el: '#app',
data: {
myMessage: '',
},
})
</script>
https://github.com/mjieun0956/TIL/tree/master/Vue/02.%20Directives