Skip to content

Commit 2d4e255

Browse files
author
dawpf
committed
服务器代理跨域请求设置
1 parent c428c29 commit 2d4e255

File tree

9 files changed

+44
-61
lines changed

9 files changed

+44
-61
lines changed

.env.development

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.export = {
2+
VUE_APP_URL = https://www.easy-mock.com/mock/example_dev
3+
}

.env.production

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.export = {
2+
VUE_APP_URL = https://www.easy-mock.com/mock/example_pro
3+
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.DS_Store
22
node_modules
33
/dist
4+
/devdist
45

56
# local env files
67
.env.local

src/App.vue

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,11 @@
33
<div id="nav">
44
<router-link to="/">Home</router-link>|
55
<router-link to="/about">About</router-link>
6-
<div>计算属性:{{appData}}</div>
7-
<div>调用方法:{{this.$store.state.app_data}}</div>
86
</div>
97
<router-view />
108
</div>
119
</template>
1210

13-
<script>
14-
import { mapState } from 'vuex'
15-
16-
export default {
17-
computed: {
18-
...mapState({
19-
appData: state => state.app_data
20-
})
21-
}
22-
}
23-
</script>
2411

2512
<style lang="less">
2613
#app {

src/api/home.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ import Service from "@/utils/service.js"
44
export function getMockData_home() {
55
return $HTTP.get(Service.mock)
66
}
7+
8+
export function getProxtData() {
9+
return $HTTP.get('/api/v2/movie/in_theaters')
10+
}

src/store/index.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,8 @@ import Vuex from "vuex";
44
Vue.use(Vuex);
55

66
export default new Vuex.Store({
7-
state: {
8-
app_data: 100
9-
},
10-
mutations: {
11-
add_app_data(state, num) {
12-
state.app_data += num
13-
}
14-
},
15-
actions: {
16-
add_app_data_action(content, num) {
17-
content.commit('add_app_data', num)
18-
}
19-
},
7+
state: {},
8+
mutations: {},
9+
actions: {},
2010
modules: {}
2111
});

src/utils/HTTP.js

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { getToken } from '@/utils/auth'
44
// create an axios instance
55
const request = axios.create({
66
// baseURL: process.env.VUE_APP_URL,
7-
baseURL: 'https://easy-mock.bookset.io/mock/5e90379d00bfc566e5bb1acb/example',
7+
// baseURL: 'https://easy-mock.bookset.io/mock/5e90379d00bfc566e5bb1acb/example', // 测试用地址
8+
baseURL: process.env.NODE_ENV === "development" ? '' : process.env.VUE_APP_URL, // 使用代理时,baseURL 在本地服务器设置为空
89
// withCredentials: true, // 跨域请求时发送cookie
910
timeout: 60000
1011
})
@@ -15,27 +16,24 @@ const TOKEN = getToken() // 获取token
1516
request.interceptors.request.use(
1617
config => {
1718
if (TOKEN) {
18-
// let each request carry token
19-
// ['X-Token'] is a custom headers key
20-
// please modify it according to the actual situation
2119
config.headers['Authorization'] = TOKEN
2220
}
2321

2422
// 扩展管理--expands:[]
25-
if (config.params && config.params.expandList) {
26-
const expandList = config.params.expandList
27-
const expands = {}
28-
if (expandList && expandList.length) {
29-
expandList.map((item, index) => {
30-
expands[`expands[${index}]`] = item
31-
})
32-
config.params = {
33-
...config.params,
34-
...expands
35-
}
36-
}
37-
delete config.params.expandList
38-
}
23+
// if (config.params && config.params.expandList) {
24+
// const expandList = config.params.expandList
25+
// const expands = {}
26+
// if (expandList && expandList.length) {
27+
// expandList.map((item, index) => {
28+
// expands[`expands[${index}]`] = item
29+
// })
30+
// config.params = {
31+
// ...config.params,
32+
// ...expands
33+
// }
34+
// }
35+
// delete config.params.expandList
36+
// }
3937

4038
return config
4139
},

src/views/Home.vue

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
<template>
22
<div class="home">
33
<img alt="Vue logo" src="../assets/logo.png" />
4-
<div>
5-
<button style="border:1px solid #000" @click="btnClick">点击增加20</button>
6-
</div>
7-
<div>
8-
<button style="border:1px solid #000" @click="btnClick1">点击增加30</button>
9-
</div>
4+
105
<HelloWorld msg="Welcome to Your Vue.js App" />
116
</div>
127
</template>
@@ -15,24 +10,17 @@
1510
// @ is an alias to /src
1611
import HelloWorld from '@/components/HelloWorld.vue'
1712
18-
import { getMockData_home } from '@/api/home.js'
13+
import { getMockData_home, getProxtData } from '@/api/home.js'
1914
2015
export default {
2116
name: 'Home',
2217
components: {
2318
HelloWorld
2419
},
2520
async created() {
26-
let res = await getMockData_home() // 发起请求
27-
console.log('获取到的请求回来的数据为:', res)
28-
},
29-
methods: {
30-
btnClick() {
31-
this.$store.commit('add_app_data', 20)
32-
},
33-
btnClick1() {
34-
this.$store.dispatch('add_app_data_action', 30)
35-
}
21+
// let res = await getMockData_home() // 发起请求
22+
let res = await getProxtData()
23+
console.log('代理获取到的请求回来的数据为:', res)
3624
}
3725
}
3826
</script>

vue.config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ module.exports = {
2020
overlay: {
2121
warnings: true,
2222
errors: true
23+
},
24+
proxy: {
25+
'/api': {
26+
target: 'http://t.yushu.im', //代理的目标地址,这是豆瓣接口地址网址
27+
changeOrigin: true, //是否设置同源,输入是的
28+
pathRewrite: { //路径重写
29+
'/api': '' //选择忽略拦截器里面的单词
30+
}
31+
}
2332
}
2433
},
2534
configureWebpack: { // 覆盖webpack默认配置的都在这里

0 commit comments

Comments
 (0)