Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using custom components with Nuxt #25

Open
steakscience opened this issue Aug 1, 2023 · 3 comments
Open

Using custom components with Nuxt #25

steakscience opened this issue Aug 1, 2023 · 3 comments

Comments

@steakscience
Copy link

Hi,

I'm trying to figure out how to pass in a custom component when using Nuxt.

I've followed the instructions and created a plugin, but I'm not sure how/where to pass in the custom component

Thanks!

@joshuadempsey
Copy link

If I understand your question correctly, Nuxt autoloads components so you can just follow the install guide and then in your App.vue...

`


<button @click="() => toast('My first toast')">Render a toast

<script lang="ts" setup> import { Toaster, toast } from 'vue-sonner' </script>`

This should work.

@Heunsig
Copy link

Heunsig commented Nov 5, 2023

If you have followed Sonner's setup instructions, you may have created a plugins/sonner.client.ts file to add the Toaster component globally. The word client in the filename signifies that the component is meant to be rendered exclusively on the client side. Yet, when the Toaster is used within a .vue SFC, the server attempts to render it as well, leading to an error since theToaster is configured to render on the client-side only.

To resolve this issue, enclose Toaster component within a <ClientOnly>. This ensures that the Toaster is rendered only on the client side.

Your code would then look like the following:

<!-- App.vue -->
<template>
  <!-- existing content -->
  <ClientOnly>
    <Toaster />
  </ClientOnly>
  <button @click="() => toast('My first toast')">
    Give me a toast
  </button>
</template>

@gsabater
Copy link

@steakscience Since the answers above didn't help me. I did it this way:

Import your component anyway at the top of your plugin or vue file

import Toast from '../components/toasts/Importing.vue'

And then pass it as the first param when calling

$toast = $nuxt.$toast.info(Toast)

Hope it helps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants