mrF*_*yer 8 javascript vue.js nuxt.js
在服务器渲染代理工作正常。请求将发送至 custom-server.com/v1/places。但是在浏览器中请求将转到 current-domain.com/api/places
为什么它在浏览器中不起作用?代理仅在服务器端工作?请帮忙。
我有 NuxtJS 配置:
require('dotenv').config();
export default {
mode: 'universal',
buildModules: [],
modules: [
'@nuxtjs/axios',
'@nuxtjs/proxy',
['@nuxtjs/dotenv', { systemvars: true }],
],
axios: {
proxy: true,
credentials: true,
},
proxy: {
'/api': {
target: "http://custom-server.com",
pathRewrite: {
'^/api' : "/v1"
},
changeOrigin: true,
},
},
}
Run Code Online (Sandbox Code Playgroud)
我的组件:
<script>
export default {
data() {
return{
placesServer:false,
placesBrowser:false,
}
},
async asyncData ({ $axios }) {
// Here is all is fine
let response = await $axios.get("/api/places");
return {
placesServer:response.data,
};
},
created(){
if (process.browser){
// Here is not working =(
this.$axios.get("/api/places").then((response)=>{
this.placesBrowser = response.data;
});
}else{
// Here is working fine!
this.$axios.get("/api/places").then((response)=>{
this.placesBrowser = response.data;
});
}
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
小智 -1
如果您的 API URL 是 =http://custom-server.com/api/v1/api/places
需要跟踪给定代码的更改并需要了解 vuejs/Nuxtjs 生命周期
export default {
mode: 'universal',
buildModules: [],
modules: [
'@nuxtjs/axios',
'@nuxtjs/proxy',
['@nuxtjs/dotenv', { systemvars: true }],
],
axios: {
proxy: true,
},
proxy: {
'/api': {
target: "http://custom-server.com",
pathRewrite: {
'^/api' : ""
},
},
},
}
Run Code Online (Sandbox Code Playgroud)
并且created()钩子内的给定代码可能需要改变另一个生命周期。或需要移动到 method() 内部或根据您的要求。
归档时间: |
|
查看次数: |
3049 次 |
最近记录: |