Nuxt/test/fixtures/basic/pages/fetch.vue
Pooya Parsa 2015140d12 feat(vue-app): universal fetch (#5028)
* pkg(nuxt-start): add node-fetch, unfetch

* pkg(vue-app): add node-fetch, unfetch

* add yarn.lock

* feat(config): _app.fetch options

* feat(builder): add fetch to templateVars

* feat(vue-app): polyfill global with fetch

* feat(fixtures/basic): /api/test

* add fetch example to fixtures

* remove unfetch from nuxt-start

* update template snapshot

* revert extra new line in server.js

* single line if
2019-02-14 16:56:58 +01:00

35 lines
617 B
Vue

<template>
<div>
{{ data }}
<button @click="update">
Fetch
</button>
<button @click="reload">
Reload
</button>
</div>
</template>
<script>
const name = process.server ? 'server' : 'client'
const baseURL = 'http://localhost:3000/api'
const getData = () => fetch(`${baseURL}/test`)
.then(r => r.text())
.then(r => r + ` (From ${name})`)
export default {
async asyncData() {
const data = await getData()
return { data }
},
methods: {
async update() {
this.data = await getData()
},
reload() {
window.location.reload()
}
}
}
</script>