feat: add axios and proxy example

This commit is contained in:
Clark Du 2017-10-29 17:08:42 +08:00
parent c0065127f1
commit 8f14fdfd82
No known key found for this signature in database
GPG Key ID: D0E5986AF78B86D9
4 changed files with 72 additions and 0 deletions

32
examples/axios/README.md Normal file
View File

@ -0,0 +1,32 @@
# Axios Proxy Example
## Install
```bash
$ yarn add @nuxtjs/axios @nuxtjs/proxy
```
## Nuxt.config.js
```json
{
modules: [
'@nuxtjs/axios',
'@nuxtjs/proxy'
],
proxy: [
['/api/dog', { target: 'https://dog.ceo/', pathRewrite: { '^/api/dog': '/api/breeds/image/random' } }]
]
}
```
### Use Axios
```js
async asyncData({ app }) {
const ip = await app.$axios.$get('http://icanhazip.com')
return { ip }
}
```
More detail, please refer [axios-module](https://github.com/nuxt-community/axios-module).

View File

@ -0,0 +1,9 @@
module.exports = {
modules: [
'@nuxtjs/axios',
'@nuxtjs/proxy'
],
proxy: [
['/api/dog', { target: 'https://dog.ceo/', pathRewrite: { '^/api/dog': '/api/breeds/image/random' } }]
]
}

View File

@ -0,0 +1,14 @@
{
"name": "nuxt-proxy",
"version": "1.0.0",
"dependencies": {
"@nuxtjs/axios": "^4.4.0",
"@nuxtjs/proxy": "^1.1.2",
"nuxt": "latest"
},
"scripts": {
"dev": "nuxt",
"build": "nuxt build",
"start": "nuxt start"
}
}

View File

@ -0,0 +1,17 @@
<template>
<div>
<h1>Dog</h1>
<img :src="dog" />
</div>
</template>
<script>
export default {
async asyncData ({ app }) {
const { data: { message: dog } } = await app.$axios.get('/dog')
return { dog }
}
}
</script>