Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- EventSource
- SSE
- 성장기
- 실시간알림
- node.js
- 개발자
- frontend
- ServerSentEvent
- Node.js기본
- 열공하자
- partitioning
- localStorage
- Lag
- node.js란
- Partition
- mariadb
- PostgreSQL
- 파티셔닝
Archives
- Today
- Total
써치킴의 우당탕탕 개발 블로그
[Vue.js][Ch2][Vue 문법] 컴포넌트 - Refs 본문
Ref
vue.js에서 제공하는 Refence의 약어로 '요소를 참조하겠다'는 의미이다.
컴포넌트가 HTML 구조와 연결된 직후에만 사용 가능하다.
=> mounted 라이프 사이클에서 사용 가능, created 라이프 사이클에서 사용 불가능
App.vue
<template>
<!-- ref : reference의 약어 -->
<h1 ref="hello"> <!-- h1 태그 요소를 hello라는 이름으로 참조하겠다. -->
Hello World!
</h1>
</template>
<script>
export default {
created() { // 컴포넌트가 생성된 직후
console.log(this.$refs.hello);
},
mounted() { // 컴포넌트가 HTML 구조와 연결된 직후
console.log(this.$refs.hello);
console.log(this.$refs.hello.textContent);
}
}
</script>
출력
컴포넌트를 ref를 이용해서 참조할 경우
최상위 요소가 1개일 때 콘솔에 뽑아보면
Target > $el은 최상위 요소를 의미한다.
App.vue
<template>
<Hello ref="hello" />
</template>
<script>
import Hello from '~/components/Ex18_Hello'
export default {
components : {
Hello
},
mounted() { // 컴포넌트가 HTML 구조와 연결된 직후
console.log(this.$refs.hello.$el);
}
}
</script>
Hello.vue
<template>
<h1>Hello~</h1>
</template>
출력
최상위 요소가 여러 개일 때
하위 컴포넌트의 특정 요소에 ref 속성을 추가하고 상위 컴포넌트에서 $refs를 사용
App.vue
<template>
<Hello ref="hello" />
</template>
<script>
import Hello from '~/components/Ex18_Hello'
export default {
components : {
Hello
},
mounted() { // 컴포넌트가 HTML 구조와 연결된 직후
console.log(this.$refs.hello.$refs.good);
}
}
</script>
Hello.vue
<template>
<h1>Hello~</h1>
<h1 ref="good">Good?</h1>
</template>
출력
'완벽하게 Vue.js' 카테고리의 다른 글
[Vue.js][Ch3][컴포지션API] 기본 옵션과 라이프사이클 (0) | 2022.04.20 |
---|---|
[Vue.js][Ch3][컴포지션API] 반응형 데이터(반응성) (0) | 2022.04.19 |
[Vue.js][Ch2][Vue 문법] 컴포넌트 - Provide, Inject (0) | 2022.04.19 |
[Vue.js][Ch2][Vue 문법] 컴포넌트 - Slot (0) | 2022.04.18 |
[Vue.js][Ch2][Vue 문법] 컴포넌트 - Emit (0) | 2022.04.18 |
Comments