Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nuxt env 调整 #4

Open
Hareis opened this issue Dec 19, 2019 · 0 comments
Open

Nuxt env 调整 #4

Hareis opened this issue Dec 19, 2019 · 0 comments

Comments

@Hareis
Copy link
Owner

Hareis commented Dec 19, 2019

在nuxt中使用axios插件
在plugins插件中加入axios.js

...
  var baseURL =context.baseUrl|| env.baseUrl
  console.log('baseURL=', baseURL)
  if(process.server){
    $axios.setBaseURL(baseURL)
  }else{
    $axios.setBaseURL(window.location.origin)
  }
...

这里会对不同环境做判断

  • 起因:asyncData 中使用axios 默认使用是本地的80端口,(这货估计压根没想到使用端口访问的问题,以为大家都是Nginx 做反向代理)死活调不通API , 正式环境的请求中出现 http://localhost:3000 这种请求

  • 肯定不能在代码中写死线上地址的API ,于是换到在packa.json 中使用 环境变量的方式,改为如下

...
  "scripts": {
    "dev": "cross-env NODE_ENV=development REMOTE_URL=http://线上IP:5000 BASE_URL=http://localhost:3000  nodemon server/index.js --watch server",
    "build": "cross-env NODE_ENV=production REMOTE_URL=http://127.0.0.1:5000 BASE_URL=http://127.0.0.1:3000 nuxt build",
    "start": "cross-env NODE_ENV=production REMOTE_URL=http://127.0.0.1:5000 BASE_URL=http://127.0.0.1:3000 node server/index.js",
    "generate": "nuxt generate"
  },
...

使用 yarn build后一切恢复正常

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant