mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-30 09:27:13 +00:00
Merge pull request #1974 from clarkdo/dev
feat: add axios and proxy example
This commit is contained in:
commit
44e3407a6b
32
examples/axios/README.md
Normal file
32
examples/axios/README.md
Normal 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).
|
9
examples/axios/nuxt.config.js
Normal file
9
examples/axios/nuxt.config.js
Normal 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' } }]
|
||||
]
|
||||
}
|
14
examples/axios/package.json
Normal file
14
examples/axios/package.json
Normal 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"
|
||||
}
|
||||
}
|
17
examples/axios/pages/index.vue
Normal file
17
examples/axios/pages/index.vue
Normal 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>
|
Loading…
Reference in New Issue
Block a user