解决跨域
安装:
npm i -D http-proxy-middleware
src\setupProxy.js
const { createProxyMiddleware: proxy } = require('http-proxy-middleware');
module.exports = app => {
app.use('/api', proxy({
target: 'https://api.iynn.cn/film',
changeOrigin: true,
pathRewrite: {}
}))
}
封装axios请求
src\utils\http.js
import axios from 'axios'
const instance = axios.create({
timeout: 10000
});
instance.interceptors.response.use(res => res.data, err => Promise.reject(err))
export const get = (url, config = {}) => instance.get(url, config)
export const post = (url, data = {}, config = {}) => instance.get(url, data, config)
export const put = (url, data = {}, config = {}) => instance.put(url, data, config)
export const del = (url, data = {}, config = {}) => instance.delete(url, data, config)
src\api\config\filmConfig.js
const config = {
film: '/api/v1/getNowPlayingFilmList?cityId=110100&pageNum=1&pageSize=10'
}
export default config
src\api\filmApi.js
import { get } from '../utils/http'
import config from './config/filmConfig'
export const getFilmsApi = () => get(config.film)
|