vue.config.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
module.exports = {
devServer: {
open: true,
host: 'localhost',
port: 8080,
https: false, //以上的ip和端口是我们本机的;下面为需要跨域的
proxy: {
//配置跨域
'/api': {
target: 'https://chp.shadiao.app/api.php', //这里后台的地址模拟的;应该填写你们真实的后台接口
ws: true,
changOrigin: true, //允许跨域
pathRewrite: {
'^/api': '' //请求的时候使用这个api就可以
}
}
}
}
}

使用

1
2
3
4
5
6
7
8
9
this.$http
.get("/api")
.then((response) => {
this.words=response.data
})
.catch((error) => {
console.log(error);
});

main.js

1
2
import axios from "axios";
Vue.prototype.$http = axios;

使用 远程数据接口代理服务

config/index.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
var path = require("path");
module.exports = {
build: {
env: require("./prod.env"),
index: path.resolve(__dirname, "../dist/index.html"),
assetsRoot: path.resolve(__dirname, "../dist"),
assetsSubDirectory: "static",
assetsPublicPath: "./",
productionSourceMap: false,
productionGzip: false,
productionGzipExtensions: ["js", "css"],
bundleAnalyzerReport: process.env.npm_config_report,
autoOpenBrowser: true,
cssSourceMap: false,
proxyTable: {
"/api": {
target: "https://chp.shadiao.app",
changeOrigin: true,
pathRewrite: {
"^/api": "",
},
},
},
},
dev: {
env: require("./dev.env"),
port: 8080,
autoOpenBrowser: true,
assetsSubDirectory: "static",
assetsPublicPath: "./",
proxyTable: {
"/api": {
target: "https://chp.shadiao.app",
secure: false,
// 开启代理,在本地创建一个虚拟服务端
changeOrigin: true,
// 是否启用websockets
ws: false,
//重写路径 比如'/api/a/b'重写为'/aaa/ccc'
pathRewrite: {
"^/api": "",
},
},
},
cssSourceMap: false,
},
};

HoneyWords:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<template>
<p class="words">{{ words }}...</p>
</template>
<script>
const API_PROXY = "https://bird.ioliu.cn/v1/?url=";
export default {
data() {
return {
words: "",
};
},
created() {
this.getHoneyWords();
},
methods: {
getHoneyWords() {
var _this = this;
this.$axios
.get(API_PROXY + "https://chp.shadiao.app/api.php")
.then(
function(res) {
console.log(res.data)
_this.words=res.data
},
function(error) {
console.log(error);
}
);
},
},
};
</script>
<style lang="scss" scope>
.words {
line-height: 40px;
padding-top: 10px;
font-size: 18px;
color: #a685e2;
font-weight: 300;
}
</style>