# ref ํƒ€์ดํ•‘

๋ทฐ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ํ”„๋กœ์ ํŠธ์—์„œ ref ์†์„ฑ์˜ ํƒ€์ž…์„ ์ •์˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ด…๋‹ˆ๋‹ค.

# ref ์†์„ฑ์ด๋ž€?

ref ์†์„ฑ์€ ๋ทฐ์—์„œ ํŠน์ • DOM์ด๋‚˜ ์ปดํฌ๋„ŒํŠธ์˜ ์ •๋ณด๋ฅผ ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ์†์„ฑ์ž…๋‹ˆ๋‹ค.

<div ref="my"></div>

<script>
new Vue({
  mounted() {
    console.log(this.$refs.my); // HTMLDivElement
  }  
})
</script>

# ref ์†์„ฑ ํƒ€์ž… ์ •์˜ ๋ฐฉ๋ฒ•

๋ทฐ ์ปดํฌ๋„ŒํŠธ ํŒŒ์ผ์—์„œ ref ์†์„ฑ์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ํƒ€์ž…์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.



ย 






ย 
ย 
ย 

ย 




<template>
  <div>
    <div ref="my"></div>
  </div>
</template>

<script lang="ts">
import Vue, { VueConstructor } from "vue";

export default (Vue as VueConstructor<
  Vue & { $refs: { my: HTMLDivElement } }
>).extend({
  mounted() {
    this.$refs.my; // HTMLDivElement
  }
});
</script>

# ๋ฐ˜๋ณต์ ์ธ ํƒ€์ž… ์ฝ”๋“œ ์ค„์ด๋Š” ๋ฐฉ๋ฒ•

์ปดํฌ๋„ŒํŠธ๋งˆ๋‹ค ๋งค๋ฒˆ ์œ„์™€ ๊ฐ™์€ ํ˜•ํƒœ์˜ ํƒ€์ž…์„ ์ •์˜ํ•˜๊ธฐ๊ฐ€ ๋ฒˆ๊ฑฐ๋กญ๋‹ค๋ฉด ์ฝ”๋“œ๋ฅผ ์ข€ ๋” ์ค„์ผ ์ˆ˜ ์žˆ๋Š” ์ œ๋„ค๋ฆญ ํƒ€์ž…์„ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค.

// src/types.ts
type MyVue<T> = VueConstructor<Vue & T>;
type MyVueRefs<T> = VueConstructor<Vue & { $refs: T }>;

// App.vue
export default (Vue as MyVueRefs<{ my: HTMLDivElement }>).extend({
  mounted() {
    this.$refs.my; // HTMLDivElement
  }
});