Simple toast notification using Vuex
- Vue >= 2.0
- Vuex >= 2.0
http://codepen.io/ktsn/pen/Bzxkjd
First, you should register a toast module to your Vuex store.
import Vue from 'vue'
import Vuex from 'vuex'
import { createModule } from 'vuex-toast'
Vue.use(Vuex)
export default new Vuex.Store({
modules: {
// ...
toast: createModule({
dismissInterval: 8000
})
// ...
}
})
Put Toast
component anywhere in your application.
<template>
<div>
<article><!-- Your contents --></article>
<toast position="ne"></toast>
</div>
</template>
<script>
import { Toast } from 'vuex-toast'
export default {
// ...
components: {
Toast
}
// ...
}
</script>
You can send notifications to the toast component with toast type.
import { mapActions } from 'vuex'
import { ADD_TOAST_MESSAGE } from 'vuex-toast'
export default {
methods: {
...mapActions({
addToast: ADD_TOAST_MESSAGE
}),
sendNotification(text) {
this.addToast({
text,
type: 'success',
dismissAfter: 10000
})
}
}
}
A Vue component that shows toast messages.
- props
- position
Create Vuex module for managing toast messages.
- options
- dismissInterval
Create customized toast component.
- options
- transition
- ADD_TOAST_MESSAGE
dispatch(ADD_TOAST_MESSAGE, { text, type, dismissAfter })
- REMOVE_TOAST_MESSAGE
dispatch(REMOVE_TOAST_MESSAGE, messageId)
- toastMessage
- get all toast messages.
id
Auto generated message IDtext
Text of the toast messagetype
Type of the toast messageinfo
,success
,warning
, ordanger
dismissAfter
Milli-second that indicates the message dismiss after this time
MIT