<div id="app">
<h1>data_01 : {{ number1 }}</h1>
<h1>data_02 : {{ number2 }}</h1>
<hr>
<h1>add_method : {{ add_method() }}</h1>
<h1>add_method : {{ add_method() }}</h1>
<h1>add_method : {{ add_method() }}</h1>
<hr>
<h1>add_computed : {{ add_computed }}</h1>
<h1>add_computed : {{ add_computed }}</h1>
<h1>add_computed : {{ add_computed }}</h1>
<hr>
<button v-on:click="dataChange">Change Data</button>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
number1: 100,
number2: 100
},
computed: {
add_computed: function () {
console.log('computed 실행됨!')
return this.number1 + this.number2
},
},
methods: {
add_method: function () {
console.log('method 실행됨!')
return this.number1 + this.number2
},
dataChange: function () {
this.number1 = 200
this.number2 = 300
},
}
})
</script>
<div id="app">
<h3>Increase number</h3>
<p>{{ number }}</p>
<button @click="number++">+</button>
<hr>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
number: 0
},
watch: {
number: function (val, oldVal) {
console.log(val, oldVal)
},
}
})
</script>
deep
속성 추가 필요<div id="app">
<h3>Change name</h3>
<p>{{ name }}</p>
<input type="text" v-model="name">
<hr>
<h3>push myObj</h3>
<p>{{ myObj }}</p>
<button @click="itemChange">change Item</button>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
number: 0,
name: '',
myObj: {completed: true}
},
methods: {
nameChange: function () {
console.log('name is changed')
},
itemChange: function () {
this.myObj.completed = !this.myObj.completed // myObj.completed 속성의 값을 반전
}
},
watch: {
name: {
handler: 'nameChange' // name 값이 변경될 때 nameChange() 메서드가 호출
},
myObj: {
handler: function (val) { // myObj 객체의 값이 변경될 때마다 호출
console.log(val) // 변경된 myObj 객체가 콘솔에 출력
},
deep: true // 객체의 속성까지 감시
},
}
})
</script>
|
(파이프)와 함께 추가되어야 함<div id="app">
<p>{{ numbers }}</p>
<p>{{ numbers | getOddNums }}</p>
<p>{{ numbers | getUnderTenNums }}</p>
<p>{{ numbers | getOddNums | getUnderTenNums }}</p>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
},
filters: {
getOddNums: function (nums) {
const oddNums = nums.filter((num) => {
return num % 2
})
return oddNums
},
getUnderTenNums: function (nums) {
const underTen = nums.filter((num) => {
return num < 10
})
return underTen
}
}
})
</script>
https://github.com/mjieun0956/TIL/tree/master/Vue/03.%20Vue%20advanced