Created
June 8, 2019 19:40
-
-
Save loloof64/f5b4b6894647bdf3b21aa27a78de1f57 to your computer and use it in GitHub Desktop.
VueJS tutorial Udemy : Components communication
This file contains hidden or 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 @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> |
This file contains hidden or 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="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> |
This file contains hidden or 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="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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment