我正在使用nuxt.config.js开发一个网站,并希望在nuxt.js中有一个有条件的配置选项:我想在运行npm运行生成(构建静态)时更改路由器库。
下面是开发环境的完整配置文件(npm运行dev):
const pkg = require('./package')
module.exports = {
mode: 'universal',
// if I uncomment the following three lines, the config is OK for npm run generate.
// router: {
// base: '/app/'
// },
/*
** Headers of the page
*/
head: {
title: pkg.name,
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' },
{ rel: 'stylesheet', href: 'https://fonts.googleapis.com/css?family=Montserrat:400,500,600&subset=latin-ext' }
]
},
/*
** Customize the progress-bar color
*/
loading: { color: '#fff' },
/*
** Global CSS
*/
css: [
'@/assets/css/main.scss',
],
/*
** Plugins to load before mounting the App
*/
plugins: [
],
/*
** Nuxt.js modules
*/
modules: [
// Doc: https://axios.nuxtjs.org/usage
'@nuxtjs/axios',
// Doc: https://bootstrap-vue.js.org/docs/
'bootstrap-vue/nuxt',
// Doc: https://github.com/vanhoofmaarten/nuxt-mq
[
'nuxt-mq',
{
// Default breakpoint for SSR
// Breakpoints are bootstrap-vue Breakpoints
// Doc: https://bootstrap-vue.js.org/docs/components/layout
defaultBreakpoint: 'default',
breakpoints: {
xs: 576, // 576 not included
sm: 768, // 768 not included
md: 992, // 992 not included
lg: 1200, // 1200 not included
xl: Infinity
}
}
]
],
bootstrapVue: {
bootstrapCSS: false, // or `css`
bootstrapVueCSS: false // or `bvCSS`
},
/*
** Axios module configuration
*/
axios: {
// See https://github.com/nuxt-community/axios-module#options
},
serverMiddleware: [
'~/api/contact'
],
/*
** 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)/
})
}
}
}
}配置对于这两种设置都很好(所以它编译了,应用程序运行正常),但是我想让它自动运行,因为当我想查看静态版本时,我经常忘记取消对路由器设置的注释。
我对这个问题看得不多,只是读了一些这样的问题,并在谷歌上搜索了一下(比如:nuxt.js -> Howto configure production/development settings或https://github.com/nuxt/nuxt.js/issues/2940)。
发布于 2019-03-26 09:34:49
您可以使用一个环境变量,并在您的配置文件中包含这个环境变量的条件:
const pkg = require('./package')
let config = {
mode: 'universal',
head: {},
...
}
if (process.env.NODE_GENERATION_TYPE === 'static') {
config.router = {
base: '/app/'
}
}
module.exports = config然后,您需要使用以下命令行来生成静态网站:
NODE_GENERATION_TYPE=static npm run generate您还可以在package.json中设置一个脚本,使其更漂亮:
{
"scripts": {
"generate:static": "NODE_GENERATION_TYPE=static dev",
"dev": "..."
},
...
}然后您就可以使用npm run generate:static运行它了。
发布于 2021-12-22 21:59:39
您还可以使用ES6对象扩展来实现:
export default {
// Disable server-side rendering: https://go.nuxtjs.dev/ssr-mode
ssr: false,
...process.env.NODE_ENV !== 'development' && { router: { base: '/app/'} },我还遇到一种情况,即模块只需要在生产环境中导入:
// Modules: https://go.nuxtjs.dev/config-modules
modules: [
// https://go.nuxtjs.dev/axios
'@nuxtjs/axios',
(process.env.NODE_ENV !== 'development' ? 'my-module' : function() {}),
// In case it accepts arguments
(process.env.NODE_ENV !== 'development' ? ['@jabardigitalservice/nuxt-module-keycloak', {
namespace: 'namespace',
clientId: 'client',
realm: 'realm',
keycloakUrl: 'keycloak',
redirectUri: 'redirectUri'
}] : [function() {}])
...https://stackoverflow.com/questions/55353726
复制相似问题