Skip to content

Commit 76c6188

Browse files
authored
fix Nuxt Composition API (Maronato#287)
1 parent 1ad2724 commit 76c6188

File tree

5 files changed

+37
-8
lines changed

5 files changed

+37
-8
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Wanna try it out? Check out the [live demo](https://maronato.github.io/vue-toast
2525
- [Usage](#usage)
2626
- [Plugin registration](#plugin-registration)
2727
- [Nuxt registration](#nuxt-registration)
28+
- [Nuxt and Composition API](#nuxt-and-composition-api)
2829
- [Injecting the Toast CSS](#injecting-the-toast-css)
2930
- [Composition API registration](#composition-api-registration)
3031
- [Generic registration](#generic-registration)
@@ -204,6 +205,22 @@ If you are using Typescript with Nuxt, you may need to add `"vue-toastification/
204205
}
205206
```
206207

208+
#### Nuxt and Composition API
209+
Since Vue Toastification is auto installed with nuxt, we need not provide it a second time.
210+
211+
To access your `$toast` instance from within `setup()`, import from `vue-toastification/composition/nuxt` like so:
212+
```ts
213+
// MyComponent.vue
214+
215+
// Import from vue-toastification/composition/nuxt, not vue-toastification/composition
216+
import { useToast } from "vue-toastification/composition/nuxt";
217+
218+
// Then, in the setup method
219+
setup() {
220+
const toast = useToast()
221+
// Use it like you would use this.$toast
222+
}
223+
```
207224

208225
#### Injecting the Toast CSS
209226
By default, when you register the module within Nuxt it automatically injects the CSS required to display the toasts using the default `vue-toastification/dist/index.css` file.

composition/index.d.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import _Vue from "vue";
2-
import { VueConstructor } from "vue/types/vue";
3-
import { PluginOptions } from "vue-toastification/dist/types/src/types";
4-
import ToastInterface from "vue-toastification/dist/types/src/ts/interface";
1+
import _Vue from "vue"
2+
import type { VueConstructor } from "vue/types/vue"
3+
import type { PluginOptions } from "../src/types"
4+
import type ToastInterface from "../src/ts/interface"
55

6-
declare let provideToast: (options?: PluginOptions) => void;
6+
declare let provideToast: (options?: PluginOptions) => void
77
declare let useToast: (
88
eventBus?: InstanceType<VueConstructor>
9-
) => ReturnType<typeof ToastInterface>;
9+
) => ReturnType<typeof ToastInterface>
1010

11-
export { provideToast, useToast };
11+
export { provideToast, useToast }

composition/nuxt/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type ToastInterface from "../../src/ts/interface";
2+
3+
declare let useToast: () => ReturnType<typeof ToastInterface>;
4+
5+
export { useToast };

composition/nuxt/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const { useContext } = require("@nuxtjs/composition-api"); // eslint-disable-line @typescript-eslint/no-var-requires
2+
3+
export const useToast = () => useContext().app.$toast;
4+
5+
module.exports = { useToast };

nuxt/index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
declare const toastModule: import("@nuxt/types").Module<import("../src/types/index").NuxtModuleOptions>;
1+
declare const toastModule: import("@nuxt/types").Module<
2+
import("../src/types/index").NuxtModuleOptions
3+
>;
24
export = toastModule;

0 commit comments

Comments
 (0)