vue-sonner icon indicating copy to clipboard operation
vue-sonner copied to clipboard

Using custom components with Nuxt

Open steakscience opened this issue 2 years ago • 3 comments

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!

steakscience avatar Aug 01 '23 12:08 steakscience

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

`

`

This should work.

joshuadempsey avatar Aug 20 '23 12:08 joshuadempsey

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>

Heunsig avatar Nov 05 '23 10:11 Heunsig

@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

gsabater avatar Jan 24 '24 16:01 gsabater