Vue Study

Vue3 - teleport

eddie0329 2020. 11. 8. 13:24
๋ฐ˜์‘ํ˜•

๐Ÿ“Œ ๋ชฉ์ฐจ

  • Introduction
  • What is teleport?
  • Usage
  • Conclusion
  • Reference

๐Ÿ“Œ Introduction

์•ˆ๋…•ํ•˜์„ธ์š”. ์ด๋ฒˆ์—๋Š” teleport์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ์ฝ”๋“œ๋Š” ์—ฌ๊ธฐ์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ“Œ What is teleport?

teleport๋Š” vue2์—์„œ vue-teleport์™€ ๋˜‘๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ฌธ์„œ์—์„œ๋Š” ์ฃผ๋กœ modal์„ ์‚ฌ์šฉํ• ๋•Œ ์“ด๋‹ค๊ณ  ํ•˜๋„ค์š”.

<template>
  <!-- ok -->
  <teleport to="#some-id" />
  <teleport to=".some-class" />
  <teleport to="[data-teleport]" />

  <!-- Wrong -->
  <teleport to="h1" />
  <teleport to="some-string" />
</template>

to๋ฅผ ์ด์šฉํ•ด์„œ ๋ณด๋‚ผ์ˆ˜ ์žˆ๋Š”๋ฐ ๊ทธ๋ƒฅ ์ผ๋ฐ˜์ ์ธ string์œผ๋กœ๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์•„์ด๋””๋‚˜ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์‚ฌ์šฉํ•˜๋ผ๊ณ  ํ•˜๋„ค์š”. ๋‹น์—ฐํ•œ ์–˜๊ธฐ์ง€๋งŒ ์›ํ•˜๋Š” ๊ณณ์œผ๋กœ ์ œ๋Œ€๋กœ ๋ณด๋‚ผ์ˆ˜ ์žˆ์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

๐Ÿ“Œ Usage


// App.js
<template>
  <div id="teleport"></div>
  <MyDiv01 />
  <MyDiv02 />
</template>

<script>
import MyDiv01 from './components/MyDiv01';
import MyDiv02 from './components/MyDiv02';

export default {
  name: 'App',
  components: {
    MyDiv01,
    MyDiv02
  }
}
</script>
<template>
  <teleport to="#teleport">
    <div>MyDiv01</div>
  </teleport>
</template>
<template>
    <teleport to="#teleport">
    <div>MyDiv02</div>
  </teleport>
</template>

์ด์ œ div(#teleport)์•ˆ์œผ๋กœ ๋“ค์–ด๊ฐ€์ง‘๋‹ˆ๋‹ค.

๐Ÿ“Œ Conclusion

vue3์—์„œ teleport๋ฅผ ์ง€์›ํ•ด์ค€๋‹ค๋‹ˆ ์ •๋ง ์ข‹์€ ์†Œ์‹์ด์—ˆ๋˜๊ฑฐ ๊ฐ™์•„์š”. ๊ธฐ์กด์— vue์—์„œ ํ…”๋ ˆํฌํŠธ๋ฅผ ์ด์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋”ฐ๋กœ lib๋ฅผ ์„ค์น˜ํ•ด์•ผํ–ˆ๋Š”๋ฐ ์ด์ œ ๊ทธ๋Ÿฌ์ง€ ์•Š์•„๋„ ๋œ๋‹ค๋Š”๊ฒŒ ๋„ˆ๋ฌด ์‹ ์ด ๋‚ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ๋‹ค์Œ ํฌ์ŠคํŒ…์„ ๊ธฐ๋Œ€ํ•ด์ฃผ์„ธ์š”!

๐Ÿ“Œ Reference

๋ฐ˜์‘ํ˜•