Created
July 28, 2021 13:19
-
-
Save letswritetw/cc57b6032b9733b2d19bf0afa841ebda to your computer and use it in GitHub Desktop.
vue3-composition-api-import
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> | |
<ul> | |
<li v-for="u in users" :key="u.id">{{ u.name }}</li> | |
</ul> | |
</div> | |
</template> | |
<script> | |
import { ref } from 'vue' | |
export default { | |
name: 'HelloWorld', | |
setup() { | |
const users = ref(null); | |
const getData = async () => { | |
try { | |
let data = await fetch('https://jsonplaceholder.typicode.com/users'); | |
if(!data.ok) { | |
throw Error('fetch data 失敗'); | |
} | |
users.value = await data.json(); | |
} catch(error) { | |
throw Error(error) | |
} | |
} | |
getData(); | |
return { users } | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment