一、反向代理
1、下载 nuxtjs/proxy
使用 npm
npm install @nuxtjs/proxy
或使用 yarn
yarn add @nuxtjs/proxy
2、配置 nuxt.config.js 文件
export default {
modules: [
'@nuxtjs/axios',
'@nuxtjs/proxy'
],
axios: {
baseURL: '/',
proxy: true
},
proxy: {
'/api/': {
target: 'https://i.maoyan.com',
changeOrigin: true
}
}
}
3、使用示例
<template>
<div>
<h1>User _id</h1>
<h2>{{ userInfo }}</h2>
</div>
</template>
<script>
import axios from 'axios'
export default {
layout: 'blog',
async asyncData({ params })
return {
userInfo: await axios
.get(
process.server
? 'https://i.maoyan.com/api/mmdb/movie/v3/list/hot.json?ct=%E5%B9%BF%E5%B7%9E&ci=20&channelId=4'
: '/api/mmdb/movie/v3/list/hot.json?ct=%E5%B9%BF%E5%B7%9E&ci=20&channelId=4'
)
.then((res) => res.data.data),
}
},
}
</script>