vue3 | toastify插件 | setup语法糖 | 函数式调用

680 阅读2分钟

vue3-toastify插件实现toast功能

官网:vue3-toastify.netlify.app/get-started…

image.png

效果图

toast.gif

demo

安装

npm install --save vue3-toastify

使用

在src-utils-extract.js文件中封装方法:

image.png

// 调用toast(error)
export function Toast(data) {
  return toast(data, {
    theme: 'auto',
    type: 'error',
    position: 'top-left',
    autoClose: 1500,
    transition: 'zoom',
    limit: 1,
    dangerouslyHTMLString: true
  })
}

在需要引用的vue文件里面 :

import { ref } from 'vue'
import { Toast } from '@/utils/extract'

const msg = ref('toast内容')
Toast(msg.value)

更多参数

image.png

Container Props

PropsTypeDefault ValueDescription
multiplebooleantruedisplay multiple at the same time
limitnumber-limit the number of toast displayed at the same time
newestOnTopbooleanfalseDisplay newest toast on top
containerClassNamestring-Add optional classes to the container
dangerouslyHTMLStringbooleanfalserender unsafe string, like html tag
clearOnUrlChangebooleantrueclear all toasts on url change
iconIconType-Used to display a custom icon. Set it to false to prevent
rtlbooleanfalseSupport right to left content
containerIdIdtoast.POSITION.TOP_RIGHTUsed to identify the Container when working with multiple container. Also used to set the id attribute
positionToastPositiontoast.POSITION.TOP_RIGHTOne of top-right, top-center, top-left, bottom-right, bottom-center, bottom-left
autoClosenumber\boolean5000Delay in ms to close the toast. If set to false, the notification needs to be closed manually
closeButtonVNode\booleandefault close iconReplace the default close button or false to hide the button
transitionToastTransition\CSSTransitionPropstoast.TRANSITIONS.BounceA reference to a valid transition animation
hideProgressBarbooleanfalseDisplay or not the progress bar below the toast(remaining time)
pauseOnHoverbooleantrueKeep the timer running or not on hover
pauseOnFocusLossbooleantruePause the timer when the window loses focus
closeOnClickbooleantrueDismiss toast on click
toastClassNamestring-Add optional classes to the toast
bodyClassNamestring-Add optional classes to the toast body
styleCSSProperties-Add optional inline style to the container
progressClassNamestring-Add optional classes to the progress bar
progressStyleCSSProperties-Add optional inline style to the progress bar
rolestringalertDefine the ARIA role for the toasts
themeToastThemeautoOne of auto, light, dark, colored, auto means automatically detects system theme colors

image.png

Toast Props

PropsTypeDefault ValueDescription
toastIdId-Provide a custom id
updateIdId-Used during update
dataT-any additional data you want to pass toast("hello", { data: {key: value} } as T)
typeToastType'default'One of info, success, warning, error, default
delaynumber-Let you delay the toast appearance. Pass a value in ms
onOpen() => void-Called when the notification appear
onClose() => void-Called when the notification disappear
onClick(event: MouseEvent) => void-Called when click inside Toast notification
toastStyleCSSProperties-Add optional inline style to the toast wrapper
progressnumber-Set the percentage for the controlled progress bar. Value must be between 0 and 1.
renderToastContent-Only available when using toast.update
isLoadingbollean-Only available when using `toast.loading'
dangerouslyHTMLStringbooleanfalserender unsafe string, like html tag
iconIconType-Used to display a custom icon. Set it to false to prevent
rtlbooleanfalseSupport right to left content
containerIdIdtoast.POSITION.TOP_RIGHTUsed to identify the Container when working with multiple container. Also used to set the id attribute
positionToastPositiontoast.POSITION.TOP_RIGHTOne of top-right, top-center, top-left, bottom-right, bottom-center, bottom-left
autoClosenumber\boolean5000Delay in ms to close the toast. If set to false, the notification needs to be closed manually
closeButtonVNode\booleandefault close iconReplace the default close button or false to hide the button
transitionToastTransition\CSSTransitionPropstoast.TRANSITIONS.BounceA reference to a valid transition animation
hideProgressBarbooleanfalseDisplay or not the progress bar below the toast(remaining time)
pauseOnHoverbooleantrueKeep the timer running or not on hover
pauseOnFocusLossbooleantruePause the timer when the window loses focus
closeOnClickbooleantrueDismiss toast on click
toastClassNamestring-Add optional classes to the toast
bodyClassNamestring-Add optional classes to the toast body
styleCSSProperties-Add optional inline style to the container
progressClassNamestring-Add optional classes to the progress bar
progressStyleCSSProperties-Add optional inline style to the progress bar
rolestringalertDefine the ARIA role for the toasts
themeToastThemeautoOne of auto, light, dark, colored, auto means automatically detects system theme colors