Skip to content

Instantly share code, notes, and snippets.

@RTLer
Last active March 25, 2019 01:59
Show Gist options
  • Save RTLer/a3e5897ddf05b68abada68b42ef430ce to your computer and use it in GitHub Desktop.
Save RTLer/a3e5897ddf05b68abada68b42ef430ce to your computer and use it in GitHub Desktop.
vueJs flash message component (alert)
Vue.component('child', {
ready(){
// send flash message
this.$root.$broadcast('flashMessage',{
text: 'Better check yourself, you\'re not looking too good.',
type: 'warning',//optional
important: false,//optional
timeout: 5000//optional
});
}
});
import flashMessage from 'flash-message.vue';
new Vue({
el: 'body',
components:{
flashMessage
}
});
<template>
<div class="alert alert-dismissible alert-{{message.type}}"
role="alert"
v-for="message in messages | filterBy true in 'show'"
transition="fade"
>{{message.handleShowEvent()}}
<button type="button" class="close" v-show="message.important" @click="message.remove()">
<span aria-hidden="true">&times;</span>
</button>
<strong>{{message.type|capitalize}}!</strong> {{message.text}}
</div>
</template>
<style>
</style>
<script>
export default{
name: 'flash-message',
data() {
return {
messages: []
};
},
methods: {
addMessage(message){
{
this.messages.push({
text: message.text,
type: (typeof message.type == 'undefined') ? 'info' : message.type,
show: (typeof message.show == 'undefined') ? true : message.show,
timeout: (typeof message.timeout == 'undefined') ? 3000 : message.timeout,
important: (typeof message.important == 'undefined') ? false : message.important,
remove(){
this.show = false;
},
handleShowEvent(){
{
if (!this.important) {
setTimeout(
() => this.remove(),
this.timeout
)
}
}
}
});
}
},
},
events: {
flashMessage(message) {
this.addMessage(message)
}
}
}
</script>
<!DOCTYPE html>
<html lang="en">
<body>
<div class="container">
<flash-message></flash-message>
<div class="row">
<div class="col-lg-12">
<router-view></router-view>
</div>
</div>
</div>
<script src="app.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment