【问题标题】:Nuxt.js - How to set API base URL?Nuxt.js - 如何设置 API 基本 URL?
【发布时间】:2019-12-01 08:11:19
【问题描述】:

我正在尝试在 Nuxt.js 中设置 API 基本 URL。我使用了来自 this question 的 DreaMinder 的回答。

我没有收到任何错误,但是当我尝试从我的应用程序中访问任何端点时,我会收到 404 错误(存在的端点)。我可以让我的代码工作的唯一方法是将完整的 URL 放入每个 axios 请求中。

这是我的nuxt.config.js

import pkg from './package'
require('dotenv').config()

let development = process.env.NODE_ENV !== 'production'

export default {
  mode: 'universal',

  /*
   ** Headers of the page
   */
  head: {
    // title: pkg.name,
    title: "AppName",
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      { hid: 'description', name: 'description', content: pkg.description }
    ],
    link: [{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }]
  },

  /*
   ** Customize the progress-bar color
   */
  loading: { color: '#fff' },

  /*
   ** Global CSS
   */
  css: [],

  /*
   ** Plugins to load before mounting the App
   */
  plugins: [
    '~/plugins/vue-cookies',
    '~/plugins/vee-validate',
    '~/plugins/moment',
    '~/plugins/vue-truncate-filter',
    '~/plugins/numeral'
  ],

  /*
   ** Axios module configuration
   */
  axios: {
    // See https://github.com/nuxt-community/axios-module#options
    // baseURL: process.env.API_BASE_URL || 'http://localhost:5000/v1'
    baseURL: development ? 'http://localhost:5000/v1' : 'https://api.appname.com/v1'
  },

  /*
   ** Nuxt.js modules
   */
  modules: [
    '@nuxtjs/dotenv',
    // Doc: https://axios.nuxtjs.org/usage
    '@nuxtjs/axios',
    // Doc: https://buefy.github.io/#/documentation
    'nuxt-buefy'
  ],

  /*
   ** Build configuration
   */
  build: {
    /*
     ** You can extend webpack config here
     */
    extend(config, ctx) {
      // Run ESLint on save
      if (ctx.isDev && ctx.isClient) {
        config.module.rules.push({
          enforce: 'pre',
          test: /\.(js|vue)$/,
          loader: 'eslint-loader',
          exclude: /(node_modules)/
        })
      }
    }
  }
}

有人知道我做错了什么吗?

【问题讨论】:

  • 看起来该应用正在尝试使用前端 URL 而不是 API。所以不是点击http://localhost:5000/v1/resources/users/login,而是尝试使用不存在的http://localhost:3000/resources/users/login
  • 我刚刚添加了对引用的原始问题如何使用 API_URL 变量的答案。你可以这样试试。

标签: axios nuxt.js


【解决方案1】:

我有同样的错误。 但我将其修复为使用代理。 请尝试一下。 这是我的 nuxt.config.js。

... ... ...
 modules: [
    ... ... ...
    '@nuxtjs/axios',
    '@nuxtjs/proxy'
  ],
axios: {
    // See https://github.com/nuxt-community/axios-module#options
    // baseURL: process.env.API_URL || 'http://localhost:3000/',
    proxy:true,
  },
 proxy:{
    '/api/': { target: [your base url], pathRewrite: {'^/api/': ''}, changeOrigin: true }
  },
... ... ...

您可以使用它调用端点。

let data= await this.$axios.get(
          '/api/[your endpoint url],
          {
            params: {     
              ... ... ...
            },
            headers: {
              ... ... ...
            },
          }
        );

你需要安装@nuxtjs/proxy npm 模块。 请尝试使用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-19
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    相关资源
    最近更新 更多