Forked from enkot/decomposedSetupComponent.vue
Last active
September 4, 2019 10:03
-
-
Save eladcandroid/c35d5348a868dee81c995d68dda4404c to your computer and use it in GitHub Desktop.
Change to Vue Composition Api
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
// ... | |
<script> | |
import { | |
ref, | |
watch, | |
computed, | |
onMounted, | |
onUnmounted | |
} from '@vue/composition-api' | |
import { fetchUserPosts } from '@/api' | |
function useScroll() { | |
const pageOffset = ref(0) | |
const update = () => { | |
pageOffset.value = window.pageYOffset | |
} | |
onMounted(() => window.addEventListener('scroll', update)) | |
onUnmounted(() => window.removeEventListener('scroll', update)) | |
return { pageOffset } | |
} | |
function useFetchPosts(props) { | |
const isLoading = ref(false) | |
const posts = ref([]) | |
watch( | |
() => props.id, | |
async id => { | |
isLoading.value = true | |
posts.value = await fetchUserPosts(id) | |
isLoading.value = false | |
} | |
) | |
return { isLoading, posts } | |
} | |
export default { | |
props: { | |
id: Number | |
}, | |
setup(props) { | |
const { isLoading, posts } = useFetchPosts(props) | |
const count = computed(() => posts.value.length) | |
return { | |
...useScroll(), | |
isLoading, | |
posts, | |
count | |
} | |
} | |
} | |
</script> | |
// ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment