- 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="app"> <div>{{msg}}</div> <button v-on:click="changeMessage">나를 눌러주세요</button> </div> <script> const vm = new Vue({ el: '#app', data: { msg: 'message A' }, methods: { changeMessage: function() { this.msg = 'changed'; } } }) console.log('-------------') console.log(vm) console.log('-------------') </script> </body> </html>


- 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="app"> <div>{{msg}}</div> <div>{{msg2}}</div> <button v-on:click="changeMessage">나를 눌러주세요</button> </div> <script> const vm = new Vue({ el: '#app', data: { msg: 'message A' }, methods: { changeMessage: function() { this.msg = 'changed'; this.msg2 = 'changed2'; } } }) console.log('-------------') console.log(vm) console.log('-------------') </script> </body> </html>

event, lifecycle을 초기화
beforeCreate hook 실행
외부 모듈 주입, 반응성 주입
created hook 실행 = 생성된 상태
el 이라는 옵션이 있는지 없는지 확인하고 templates라는 옵션도 확인
템플릿을 렌더링하거나 컴파일
beforeMount = 연결되기 바로직전싱테, 아직 vue는 html의 어떤 요소가 연결되어있는지 모름
(vue에) 연결된상태 : 보통 연결까지 다 되어야 data를 쓸 수 있기 때문에 mounted를 자주 쓴다
data가 변경될때 다음의 hook이 존재합니다
beforUpdate = data가 변경된건 아는데 화면을 다시 그리기 직전의 상태
updated = 화면 그린 직후의 상태
beforeDestroy = 파괴직전상태 = 이때 데이터를 백업한다든가 하는 일을 한다
destroy = watch에서 해제가 됨. 반응성에서 없어짐
각 hook마다 data로 등록한 msg를 출력해보면 어떤 값이 나올까?
- html
<div id="app"> <div>{{msg}}</div> </div>
- javascript
new Vue({ el: '#app', data: { msg: 'first msg' }, create(){ this.msg = 'create' } })
<!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="app">
<div ref="div1"></div>
{{msg}}
</div>
<script>
const vm = new Vue({
el: '#app',
data: {
msg: 'hi~'
},
beforeCreate () {
console.log('beforeCreated ', this.$refs.div1)
},
created(){
console.log('created ', this.$refs.div1)
},
beforeMount(){
console.log('beforeMount ', this.$refs.div1)
},
mounted(){
console.log('mounted!! ', this.$refs.div1)
},
})
console.log('-------------')
</script>
</body>
</html>

<script>부분beforeUpdate(){ console.log('beforeupdated!!! '+ this.$refs.msg.innerText) }, updated(){ console.log('updated!!! '+ this.$refs.msg.innerText); }

- html
beforeDestroy(){ console.log('beforeDestroy '+ this.$refs.msg.innerText); }, destroyed(){ console.log('destroyed '); }
