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
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.
Vue 3.0 Teleport vs Vue 2.0 portal:
<!-- App.vue --> <template> <div> <TeleportToDestination /> </div> </template> <script> import TeleportToDestination from './TeleportToDestination.vue'; export default { components: { TeleportToDestination, }, }; </script>
Vue 3.0 Teleport use cases:
<!-- TeleportToDestination.vue --> <template> <Teleport to="body"> <div>Teleported Content</div> </Teleport> </template> <script> import { Teleport } from 'vue'; export default { components: { Teleport, }, }; </script>
Vue 3.0 Teleport for modal implementation:
<!-- Modal.vue --> <template> <Teleport to="body"> <div class="modal"> <slot></slot> </div> </Teleport> </template> <script> import { Teleport } from 'vue'; export default { components: { Teleport, }, }; </script>
Vue 3.0 Teleport with dynamic components:
<!-- DynamicComponent.vue --> <template> <Teleport :to="destination"> <div>Dynamic Content</div> </Teleport> </template> <script> import { Teleport } from 'vue'; export default { props: ['destination'], components: { Teleport, }, }; </script>
Vue 3.0 Teleport and scoped slots:
<!-- 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>
Vue 3.0 Teleport and server-side rendering:
<!-- SSRComponent.vue --> <template> <Teleport to="body"> <div>SSR Content</div> </Teleport> </template> <script> import { Teleport } from 'vue'; export default { components: { Teleport, }, }; </script>