Skip to content

Instantly share code, notes, and snippets.

@amcsi
Created June 22, 2019 11:54
Show Gist options
  • Save amcsi/b48087048bf342647a7f309b5cf06b71 to your computer and use it in GitHub Desktop.
Save amcsi/b48087048bf342647a7f309b5cf06b71 to your computer and use it in GitHub Desktop.
VueJS Multiple Logic Topics, but with object API
<template>
<div>
<template v-if="isLoading">Loading...</template>
<template v-else>
<h3>{{ post.title }}</h3>
<p>{{ post.body }}</p>
</template>
<div>Mouse is at {{ x }}, {{ y }}</div>
</div>
</template>
<script>
import { fetchPost } from "./api"
const fetchData = {
data: {
isLoading: true,
post: null
},
mounted() {
this.fetchPost()
},
watch: {
id: "fetchPost"
},
methods: {
async fetchPost() {
this.isLoading = true
this.post = await fetchPost(this.id)
this.isLoading = false
}
}
}
const mouseData = {
data: {
x: 0,
y: 0
},
mounted() {
window.addEventListener("mousemove", this.updateMouse)
},
destroyed() {
window.removeEventListener("mousemove", this.updateMouse)
},
methods: {
updateMouse(e) {
this.x = e.pageX
this.y = e.pageY
}
}
}
export default {
props: {
id: Number
},
data() {
return {
...fetchData.data,
...mouseData.data
}
},
mounted() {
fetchData.mounted.call(this)
mouseData.mounted.call(this)
},
watch: {
...fetchData.watch
},
destroyed() {
mouseData.destroyed.call(this)
},
methods: {
...fetchData.methods,
...mouseData.methods
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment