【发布时间】: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 变量的答案。你可以这样试试。