Last active
March 25, 2019 01:59
-
-
Save RTLer/a3e5897ddf05b68abada68b42ef430ce to your computer and use it in GitHub Desktop.
vueJs flash message component (alert)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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">×</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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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