Menus

QuickStart 02 - Teleport (2021.02.12) - 图1

Teleport’s Simple Usage

We can use it to create a modal box component. Modal box can be rendered in body container with its logic code written internally.

  1. <template>
  2. <button @click="openNewWorld">点击这里打开新世界</button>
  3. <teleport to="body">
  4. <div class="modal" v-show="showModal">
  5. <div class="modal__box">
  6. <div class="modal__box-content">
  7. <p>Hello, welcome to new world.</p>
  8. <p>My name is Bill Ann.</p>
  9. </div>
  10. <button class="modal__box-close" @click="closeNewWorld">关闭</button>
  11. </div>
  12. </div>
  13. </teleport>
  14. </template>
  15. <script>
  16. import { ref } from 'vue'
  17. export default {
  18. name: 'CompositionAPI',
  19. setup() {
  20. function openNewWorld() {
  21. showModal.value = true
  22. }
  23. function closeNewWorld() {
  24. showModal.value = false
  25. }
  26. let showModal = ref(false)
  27. return {
  28. showModal,
  29. openNewWorld,
  30. closeNewWorld
  31. }
  32. }
  33. }
  34. </script>
  35. <style lang="scss">
  36. .modal {
  37. width: 100%;
  38. height: 100%;
  39. background: rgba(0, 0, 0, 0.8);
  40. position: fixed;
  41. left: 0;
  42. top: 0;
  43. }
  44. .modal__box {
  45. text-align: center;
  46. width: 400px;
  47. height: 200px;
  48. line-height: 200px;
  49. background: #ffffff;
  50. border-radius: 4px;
  51. position: absolute;
  52. left: 50%;
  53. top: 50%;
  54. transform: translate(-50%, -50%);
  55. }
  56. .modal__box-content {
  57. line-height: 1;
  58. display: inline-block;
  59. }
  60. .modal__box-close {
  61. position: absolute;
  62. top: 10px;
  63. right: 10px;
  64. }
  65. </style>

Tip: You should install sass before using it.

Props: to

to can specify which container we want to inject our template into it. Its value can be a tag name, an id name or a class name etc. It’s just an element selector.

Tips:

  • The target element must exist before the component is mounted.
  • The target cannot be rendered by the component itself, and ideally should be outside of the entire Vue component tree.

Using with Vue components

We can also use a vue component as the teleport template.

  1. const app = Vue.createApp({
  2. template: `
  3. <h1>Root instance</h1>
  4. <parent-component />
  5. `
  6. })
  7. app.component('parent-component', {
  8. template: `
  9. <h2>This is a parent component</h2>
  10. <teleport to="#endofbody">
  11. <child-component name="John" />
  12. </teleport>
  13. `
  14. })
  15. app.component('child-component', {
  16. props: ['name'],
  17. template: `
  18. <div>Hello, {{ name }}</div>
  19. `
  20. })

Combined with the slot, we can encapsulate a modal box very easily.

Using multiple teleports on the same target

If we use multiple teleports on the same target, each teleport will rendered in order.

  1. <teleport to="#modals">
  2. <div>A</div>
  3. </teleport>
  4. <teleport to="#modals">
  5. <div>B</div>
  6. </teleport>
  7. <!-- result-->
  8. <div id="modals">
  9. <div>A</div>
  10. <div>B</div>
  11. </div>