JavaScript/vue.js/proxy:修订间差异
来自康健生活
< JavaScript | vue.js
| 第33行: | 第33行: | ||
| 示例 || 示例 || 示例 | | 示例 || 示例 || 示例 | ||
|} | |} | ||
== 示例 == | |||
<syntaxhighlight lang="javascript"> | |||
/** | |||
* 在生产环境 代理是无法生效的,所以这里没有生产环境的配置 | |||
*/ | |||
module.exports = { | |||
development: { | |||
target:{ | |||
'/api': { | |||
target: 'http://localhost:3000', | |||
// 多个特定路径代理到同一目标 | |||
// context: ['/docs', '/api'], | |||
// 支持https | |||
secure: false, | |||
// logLevel: 'debug' 将代理转发过程,打印在命令行中,方便调试 | |||
logLevel: 'debug', | |||
// 接口跨域 | |||
// 后端收到的请求头中的host是目标地址 target | |||
// 设置为 true, 本地就会虚拟一个服务器接收你的请求并代你发送该请求 | |||
changeOrigin: true, | |||
pathRewrite: {'^/api' : ''}, | |||
ifProxy: false, | |||
// header: { | |||
// 'Origin': '', | |||
// 'referer': '' | |||
// } | |||
}, | |||
// '/docs': { | |||
// targetHost: 'DOCSHOST', | |||
// target: 'http://localhost:3000', | |||
// secure: false, | |||
// changeOrigin: false, | |||
// pathRewrite: { '^/LOCAL': '' }, | |||
// ifProxy: false, | |||
// }, | |||
}, | |||
staticHost: `/staticHost/`, | |||
cloudStaticHost: `cloudStaticHost`, | |||
}, | |||
production: { | |||
target:{ | |||
} | |||
}, | |||
} | |||
</syntaxhighlight> | |||
== 参考 == | == 参考 == | ||
* https://webpack.docschina.org/configuration/dev-server/#devserverproxy | * https://webpack.docschina.org/configuration/dev-server/#devserverproxy | ||
2021年3月8日 (一) 11:21的最新版本
引言
let proxy = new Proxy(target, handler); // handler 不能设置为 null,会抛出错误 Cannot create proxy with a non-object as target or handler
vue.js 增加 proxy 某种程度上是为了取代 Object.defineProperty。Object.defineProperty 有许多限制(eq:无法监听属性的添加、删除、数组索引和长度的变更等),并可以支持 Map、Set、WeakMap 和 WeakSet。
代理操作
| handler.get(target,property,receiver) | 示例 | target 是指目标对象,property 是被获取的属性名 , receiver 是 Proxy 或者继承 Proxy 的对象 |
| handler.set(target, property, value, receiver) | 示例 | value 要设置的属性值 |
| handler.apply(target, thisArg, argumentsList) | 示例 | 目标对象(函数)target,被调用时的上下文对象 thisArg 以及被调用时的参数数组 argumentsList |
| handler.construct(target, argumentsList, newTarget) | 示例 | 示例 |
| handler.has(target,prop) | 示例 | 示例 |
| 示例 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
示例
/**
* 在生产环境 代理是无法生效的,所以这里没有生产环境的配置
*/
module.exports = {
development: {
target:{
'/api': {
target: 'http://localhost:3000',
// 多个特定路径代理到同一目标
// context: ['/docs', '/api'],
// 支持https
secure: false,
// logLevel: 'debug' 将代理转发过程,打印在命令行中,方便调试
logLevel: 'debug',
// 接口跨域
// 后端收到的请求头中的host是目标地址 target
// 设置为 true, 本地就会虚拟一个服务器接收你的请求并代你发送该请求
changeOrigin: true,
pathRewrite: {'^/api' : ''},
ifProxy: false,
// header: {
// 'Origin': '',
// 'referer': ''
// }
},
// '/docs': {
// targetHost: 'DOCSHOST',
// target: 'http://localhost:3000',
// secure: false,
// changeOrigin: false,
// pathRewrite: { '^/LOCAL': '' },
// ifProxy: false,
// },
},
staticHost: `/staticHost/`,
cloudStaticHost: `cloudStaticHost`,
},
production: {
target:{
}
},
}
参考
No categories