Vue.js 3.0 Tutorial

Vue.js 3.0 Component Advanced

Vue.js 3.0 Transitions & Animations

Vue.js 3.0 Reusable & Combinable

Vue.js 3.0 Advanced

Vue.js 3.0 Tools

Vue.js 3.0 Scale

Vue.js 3.0 Accessibility

Vue.js 3.0 Migrating from Vue2

Vue.js 3.0 Contribute Documentation

Vue.js 3.0 API References

Vue.js 3.0 Style Guide

Vue 3.0 Teleport

In Vue.js 3.0, there's a new feature called Teleport that allows you to render a component somewhere else in the DOM outside of its parent component. This is particularly useful for rendering components that are visually disconnected from their parents, like modals, popups, notifications, and more.

Here's a basic example of how to use Teleport:

1. Setting up your Vue.js project

First, let's make sure you have Vue 3 installed and a new project created. If you don't have it yet, you can install it and create a new project by running the following commands:

npm install -g @vue/cli
vue create my-project
cd my-project

When creating a new project, make sure to select Vue 3 when prompted.

2. Creating a modal component

Let's create a modal component that we'll teleport later. You can put this in a file like src/components/MyModal.vue.

<template>
  <div class="modal">
    <div class="modal-content">
      <slot></slot>
    </div>
  </div>
</template>

<script>
export default {
  name: 'MyModal',
}
</script>

<style scoped>
.modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background-color: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-content {
  background-color: white;
  padding: 20px;
  border-radius: 8px;
}
</style>

This is just a basic modal with some styling. The slot tag allows us to insert custom content into our modal.

3. Using the Teleport feature

Now, let's use the Teleport feature. In the component where you want to use the modal, you can do something like this:

<template>
  <div>
    <button @click="showModal = true">Open modal</button>

    <teleport to="body">
      <MyModal v-if="showModal">
        <h1>Hello from modal!</h1>
        <button @click="showModal = false">Close modal</button>
      </MyModal>
    </teleport>
  </div>
</template>

<script>
import MyModal from './MyModal.vue'

export default {
  components: {
    MyModal
  },
  data() {
    return {
      showModal: false
    }
  }
}
</script>

In this example, we're teleporting the MyModal component to the body of our document. The modal will only be visible if showModal is true.

The to attribute in the teleport tag specifies where we want to teleport our component to. It can be a CSS selector, an HTMLElement, or a function that returns an HTMLElement.

That's it! You've successfully used Vue.js 3.0's new Teleport feature.

  1. Vue 3.0 Teleport vs Vue 2.0 portal:

    • Description: Both Teleport in Vue 3.0 and portal in Vue 2.0 provide a way to render content outside the component's local DOM boundary. However, Teleport is more powerful and flexible than portal, offering improved performance and additional features.
    • Code (Vue 3.0 Teleport):
      <!-- App.vue -->
      <template>
        <div>
          <TeleportToDestination />
        </div>
      </template>
      
      <script>
      import TeleportToDestination from './TeleportToDestination.vue';
      
      export default {
        components: {
          TeleportToDestination,
        },
      };
      </script>
      
  2. Vue 3.0 Teleport use cases:

    • Description: Teleport is useful for scenarios where you need to render content at a different location in the DOM hierarchy. Common use cases include modals, tooltips, and dropdowns.
    • Code:
      <!-- TeleportToDestination.vue -->
      <template>
        <Teleport to="body">
          <div>Teleported Content</div>
        </Teleport>
      </template>
      
      <script>
      import { Teleport } from 'vue';
      
      export default {
        components: {
          Teleport,
        },
      };
      </script>
      
  3. Vue 3.0 Teleport for modal implementation:

    • Description: Teleport simplifies modal implementation by allowing you to render the modal content outside the current component and into the body or another target element.
    • Code:
      <!-- Modal.vue -->
      <template>
        <Teleport to="body">
          <div class="modal">
            <slot></slot>
          </div>
        </Teleport>
      </template>
      
      <script>
      import { Teleport } from 'vue';
      
      export default {
        components: {
          Teleport,
        },
      };
      </script>
      
  4. Vue 3.0 Teleport with dynamic components:

    • Description: Teleport can be used with dynamic components to conditionally render content at a different location based on specific conditions.
    • Code:
      <!-- DynamicComponent.vue -->
      <template>
        <Teleport :to="destination">
          <div>Dynamic Content</div>
        </Teleport>
      </template>
      
      <script>
      import { Teleport } from 'vue';
      
      export default {
        props: ['destination'],
        components: {
          Teleport,
        },
      };
      </script>
      
  5. Vue 3.0 Teleport and scoped slots:

    • Description: Teleport can work seamlessly with scoped slots, allowing you to pass data and customize the content that gets teleported.
    • Code:
      <!-- ScopedSlotComponent.vue -->
      <template>
        <Teleport to="body">
          <template #default="{ message }">
            <div>{{ message }}</div>
          </template>
        </Teleport>
      </template>
      
      <script>
      import { Teleport } from 'vue';
      
      export default {
        components: {
          Teleport,
        },
      };
      </script>
      
  6. Vue 3.0 Teleport and server-side rendering:

    • Description: Teleport is designed to work well with server-side rendering (SSR) and can be used to teleport content to the correct place in the DOM during both client and server rendering.
    • Code:
      <!-- SSRComponent.vue -->
      <template>
        <Teleport to="body">
          <div>SSR Content</div>
        </Teleport>
      </template>
      
      <script>
      import { Teleport } from 'vue';
      
      export default {
        components: {
          Teleport,
        },
      };
      </script>