vue-safe-teleport is a utility package for Vue 3 applications that enhances the built-in `<Teleport>` component to prevent common runtime errors related to target availability. It provides `<SafeTeleport>` and `<TeleportTarget>` components, ensuring that content is only teleported once its designated target DOM element is fully mounted and available. The current stable version is `0.1.2`. While the package is relatively new, its recent bug fixes (e.g., v0.1.2 for import extensions) indicate active maintenance. Key differentiators include its explicit `TeleportTarget` component for robust target registration and a fallback single-frame delay when using `SafeTeleport` with a standard DOM selector, directly addressing the "Failed to locate Teleport target with selector" issue that frequently arises from race conditions in component lifecycles. It aims to be a drop-in replacement for `<Teleport>` with added safety.
npm install vue-safe-teleportVerified import paths — ran on the pinned version, not inferred.
This code demonstrates how to install `vue-safe-teleport`, register it as a Vue plugin, and then use both `TeleportTarget` to define a persistent teleport destination and `SafeTeleport` to send dynamic content to it, preventing common "target not found" errors. It also shows using `SafeTeleport` with a standard DOM element, which introduces a single-frame delay if the target isn't immediately present.
Replace `<Teleport to="#id">` with `<SafeTeleport to="#id">`. For best results, also introduce `<TeleportTarget id="id" />` in your root component or layout.
For explicit and immediate target availability, always pair `<SafeTeleport to="#id">` with a `<TeleportTarget id="id" />` component which notifies `SafeTeleport` when it's ready.
Upgrade to `vue-safe-teleport@0.1.2` or later. Ensure your build configuration correctly handles ES modules and import paths, especially with file extensions.
Replace Vue's `<Teleport>` with `<SafeTeleport>` from `vue-safe-teleport` and ensure a `<TeleportTarget id="my-target" />` component exists and is rendered before `<SafeTeleport>` attempts to mount its content.
Verify the package is correctly installed (`pnpm i vue-safe-teleport`). Ensure you are using ESM `import` statements (e.g., `import { SafeTeleport } from 'vue-safe-teleport'`) and update to `vue-safe-teleport@0.1.2` or later to leverage import path fixes.For direct component usage, ensure `import { SafeTeleport, TeleportTarget } from 'vue-safe-teleport'` is present. If using the plugin, ensure `app.use(VueSafeTeleport)` is called, which registers them globally.