Nuxt 如何在开发或生产中设置 baseURL [英] Nuxt How to set baseURL in dev or production
问题描述
这似乎是一个简单的 Nuxt 问题,但我就是想不通.
This seems like a simple Nuxt question, but I just can't figure it out.
运行NPM run dev"时,我想将 Axios baseURL 设置为localhost/api",在NPM run generate"之后从 dist 文件夹运行时,我希望 baseURL 为/api".
When running "NPM run dev" I want to set the Axios baseURL to "localhost/api" and when running from the dist folder after "NPM run generate" I want the baseURL to be "/api".
有没有简单的解决方案?
Is there a simple solution?
推荐答案
这是通过nuxt.config.js
实现的方法:
let development = process.env.NODE_ENV !== 'production'
module.exports = {
axios: {
baseURL: development ? 'http://localhost:3001/api' : 'https://domain/api'
},
modules: [
'@nuxtjs/axios'
],
}
如您所见,您应该指定后端的完整 URL,包括域(仅 SPA 模式除外).
As you can see, you should specify full URL of your backend, including domain (except SPA-only mode).
并且不要忘记安装@nuxtjs/axios 作为依赖项来尝试示例.
And don't forget to install @nuxtjs/axios as dependency to try the example.
这篇关于Nuxt 如何在开发或生产中设置 baseURL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!