Nuxt如何在开发或生产中设置baseURL [英] Nuxt How to set baseURL in dev or production

查看:835
本文介绍了Nuxt如何在开发或生产中设置baseURL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个简单的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做到这一点的方法:

This is the way to do it through the 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆