Merge pull request #1974 from clarkdo/dev

feat: add axios and proxy example
This commit is contained in:
Sébastien Chopin 2017-10-29 10:56:47 +01:00 committed by GitHub
commit 44e3407a6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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>