首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >nuxt.config.js中的条件属性

nuxt.config.js中的条件属性
EN

Stack Overflow用户
提问于 2019-03-26 09:30:15
回答 2查看 4.3K关注 0票数 2

我正在使用nuxt.config.js开发一个网站,并希望在nuxt.js中有一个有条件的配置选项:我想在运行npm运行生成(构建静态)时更改路由器库。

下面是开发环境的完整配置文件(npm运行dev):

代码语言:javascript
复制
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 settingshttps://github.com/nuxt/nuxt.js/issues/2940)。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-03-26 09:34:49

您可以使用一个环境变量,并在您的配置文件中包含这个环境变量的条件:

代码语言:javascript
复制
const pkg = require('./package')

let config = {
  mode: 'universal',
  head: {},
  ...
}

if (process.env.NODE_GENERATION_TYPE === 'static') {
  config.router = {
    base: '/app/'
  }
}

module.exports = config

然后,您需要使用以下命令行来生成静态网站:

代码语言:javascript
复制
NODE_GENERATION_TYPE=static npm run generate

您还可以在package.json中设置一个脚本,使其更漂亮:

代码语言:javascript
复制
{
  "scripts": {
    "generate:static": "NODE_GENERATION_TYPE=static dev",
    "dev": "..."
  },
  ...
}

然后您就可以使用npm run generate:static运行它了。

票数 4
EN

Stack Overflow用户

发布于 2021-12-22 21:59:39

您还可以使用ES6对象扩展来实现:

代码语言:javascript
复制
export default {
  // Disable server-side rendering: https://go.nuxtjs.dev/ssr-mode
  ssr: false,
 ...process.env.NODE_ENV !== 'development' && { router: { base: '/app/'} },

我还遇到一种情况,即模块只需要在生产环境中导入:

代码语言:javascript
复制
// 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() {}])
...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55353726

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档