storybook/docs/snippets/vue/document-screen-fetch.3.js.mdx
2021-11-09 01:41:54 +00:00

54 lines
1.2 KiB
Plaintext

```html
<!-- YourPage.vue -->
<template>
<div v-if="!loading && data && data.subdocuments.length">
<PageLayout :user="data.user">
<DocumentHeader :document="data.document" />
<DocumentList :documents="data.subdocuments" />
</PageLayout>
</div>
<p v-if="loading">
Loading...
</p>
<p v-if="error">
There was an error fetching the data!
</p>
</template>
<script>
import { ref } from 'vue';
import PageLayout from './PageLayout';
import DocumentHeader from './DocumentHeader';
import DocumentList from './DocumentList';
export default {
name: 'DocumentScreen',
setup() {
const data = ref(null);
const loading = ref(true);
const error = ref(null);
fetch('https://your-restful-endpoint')
.then((res) => {
if (!res.ok) {
error.value = res.statusText;
}
return res;
})
.then((res) => res.json())
.then((requestData) => {
data.value = requestData;
loading.value = false;
})
.catch(() => {
error.value = 'error';
});
return {
error,
loading,
data,
};
},
};
</script>
```