Skip to content

Instantly share code, notes, and snippets.

@loloof64
Created June 8, 2019 19:40
Show Gist options
  • Save loloof64/f5b4b6894647bdf3b21aa27a78de1f57 to your computer and use it in GitHub Desktop.
Save loloof64/f5b4b6894647bdf3b21aa27a78de1f57 to your computer and use it in GitHub Desktop.
VueJS tutorial Udemy : Components communication
<template>
<div @click="updateDetails">Server #{{id}}</div>
</template>
<script>
import eventBus from '../Shared/EventBus';
export default {
props: ['id', 'status'],
created(){
eventBus.$on('resetServer', (id) => {
if (id === this.id)
{
this.status = 'Normal';
}
})
},
methods: {
updateDetails(){
eventBus.updateDetails(this.id, this.status);
}
}
}
</script>
<template>
<div class="col-xs-12 col-sm-6">
<p v-if="serverData">Server #{{ serverData.id }} - Status : {{ serverData.status }}</p>
<p v-else>Server Details are currently not updated</p>
<button v-show="serverData" @click="resetServer">Réinitialiser</button>
</div>
</template>
<script>
import eventBus from '../Shared/EventBus';
export default {
data() {
return {
serverData: undefined,
};
},
created(){
eventBus.$on('detailsUpdated', (data) => {
this.serverData = data;
});
},
methods: {
resetServer()
{
eventBus.$emit('resetServer', this.serverData.id);
this.serverData.status = 'Normal';
}
}
}
</script>
<style>
</style>
<template>
<div class="col-xs-12 col-sm-6">
<ul class="list-group">
<li
class="list-group-item"
v-for="(server_data, index) in servers" :key="index"
>
<server
:id="server_data.id"
:status="server_data.status"
/>
</li>
</ul>
</div>
</template>
<script>
import Server from './Server.vue';
export default {
data(){
return {
servers: [
{id: 1, status: 'Normal'},
{id: 2, status: 'Critical'},
{id: 3, status: 'Unknown'},
{id: 4, status: 'Normal'},
{id: 5, status: 'Critical'},
],
}
},
components: {
server: Server,
}
}
</script>
<style>
</style>
import Vue from 'vue'
const eventBus = new Vue({
methods: {
updateDetails(id, status){
this.$emit('detailsUpdated', {
id, status,
});
}
}
});
export default eventBus;
<template>
<div class="row">
<div class="col-xs-12">
<header>
<h1>Server Status</h1>
</header>
</div>
</div>
</template>
<script>
</script>
<style>
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment