Skip to content

useNotify ​

Toast notifications on top of Notivue push. error() also understands API errors, so you can pass a caught error directly.

Registered only when we.notify.enabled is true (default). Toasts are rendered by WeNotification, which must be mounted once in your app (e.g. in app.vue).

Usage ​

ts
const notify = useNotify()

notify.success('Saved')
notify.info({ title: 'Heads up', message: 'Export started' })

try {
  await useApi().$post('orders', order, { notify: false })
} catch (e) {
  notify.error(e) // shows the API `message` when present
}

Return Value ​

MethodDescription
successpush.success
errorWrapper of push.error (see below)
infopush.info
warning / warnpush.warning
promisepush.promise — a loading toast you later resolve/reject
loadpush.load
clearAllpush.clearAll — dismiss all toasts (with leave animation)
destroyAllpush.destroyAll — remove all toasts immediately

All methods except error are the Notivue functions themselves: they accept a string or a Notivue options object and return the notification handle. See the Notivue push docs for options.

error(opts) ​

opts can be a string, a Notivue options object, an Error, or an ofetch error / context. The message shown is, in order:

  1. opts.response._data.message (the Laravel error message), if present;
  2. opts.message, if opts is an Error;
  3. opts as given.

error returns nothing.

useApi already calls error() on failed responses (except 401, 404 and 422) unless the request has notify: false.

Example: promise toast ​

ts
const notify = useNotify()

const toast = notify.promise('Importing…')
try {
  await useApi().$post('imports', payload)
  toast.resolve('Import completed')
} catch (e) {
  toast.reject('Import failed')
}

Options API ​

With we.notify.optionsApi (default true) a separate this.$notify helper is available, with a different signature (info/success/warn/error(title, text?, options?)). See Options API.

Source Code ​