【问题标题】:Nuxt.js with expresss generator appNuxt.js 与快速生成器应用程序
【发布时间】:2018-05-13 00:37:31
【问题描述】:

我想将使用 express 生成器生成的 express 应用程序集成到我的 nuxt.js 应用程序中。

我找到了一个https://github.com/nuxt-community/express-template,但它只集成了一个简单的快递应用程序。

谁能给我正确的关于使用 nuxt.js 的快速应用程序(使用快速生成器生成)的教程

【问题讨论】:

    标签: node.js express vue.js nuxt.js


    【解决方案1】:

    我最近将 NUXT 添加到我的 Express.js 应用程序中,这是我的设置示例。

    https://github.com/iampaul83/express-nuxt

    NUXT.js x Express.js

    1。 express 生成器并安装 nuxt

    # express generator
    npx express-generator --pug --git express-nuxt
    cd express-nuxt
    yarn
    
    # nuxt
    yarn add nuxt @nuxtjs/axios
    yarn add --dev eslint babel-eslint eslint-friendly-formatter eslint-loader eslint-plugin-vue
    

    2。添加nuxt文件夹

    • nuxt
      • 资产
      • 组件
      • 布局
      • 中间件
      • 插件
      • 静态
      • 商店

    3。创建 nuxt.config.js

    如果要将nuxt放在子文件夹中,则需要srcDir选项

    module.exports = {
      mode: 'universal',
    
      srcDir: __dirname,
      /*
      ** Headers of the page
      */
      head: {
        title: 'nuxt',
        meta: [
          { charset: 'utf-8' },
          { name: 'viewport', content: 'width=device-width, initial-scale=1' },
          { hid: 'description', name: 'description', content: 'Nuxt.js project' }
        ],
        link: [
          { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
        ]
      },
      /*
      ** Customize the progress bar color
      */
      loading: { color: '#3B8070' },
    
      modules: [
        // Doc: https://github.com/nuxt-community/axios-module#usage
        '@nuxtjs/axios'
      ],
    
      /*
      ** Axios module configuration
      */
      axios: {
        // See https://github.com/nuxt-community/axios-module#options
      },
    
      /*
      ** Build configuration
      */
      build: {
        /*
        ** Run ESLint on save
        */
        extend (config, { isDev, isClient }) {
          if (isDev && isClient) {
            config.module.rules.push({
              enforce: 'pre',
              test: /\.(js|vue)$/,
              loader: 'eslint-loader',
              exclude: /(node_modules)/
            })
          }
        }
      }
    }
    

    4。为渲染创建 nuxt-render.js

    const { Nuxt, Builder } = require('nuxt')
    
    const config = require('./nuxt.config.js')
    config.dev = !(process.env.NODE_ENV === 'production')
    
    const nuxt = new Nuxt(config)
    
    // build nuxt
    if (config.dev) {
        const builder = new Builder(nuxt)
        builder.build().catch((error) => {
            console.log('failed to build nuxt')
            console.log(error)
        })
    }
    
    module.exports = nuxt.render
    

    5。在 app.js 中添加 nuxt 渲染中间件

    app.use(require('./nuxt/nuxt-render'));
    

    6。添加 .eslintrc.js

    module.exports = {
      root: true,
      env: {
        browser: true,
        node: true
      },
      parserOptions: {
        parser: 'babel-eslint'
      },
      extends: [
        // https://github.com/vuejs/eslint-plugin-vue#priority-a-essential-error-prevention
        // consider switching to `plugin:vue/strongly-recommended` or `plugin:vue/recommended` for stricter rules.
        'plugin:vue/essential',
      ],
      // required to lint *.vue files
      plugins: [
        'vue'
      ],
      // add your custom rules here
      rules: {}
    }
    

    7。为 nuxt 添加 .gitignore

    # Nuxt build
    .nuxt
    
    # Nuxt generate
    dist
    

    【讨论】:

    • 让我试试你的方法
    • @Paul Tsai 你似乎是可以帮助我解决这个问题的合适人选:stackoverflow.com/questions/54982462/…
    • @SylvainF 好的,让我试试
    • @PaulTsai 为什么你只想在开发环境中构建?如果您将 config.dev 设置为 false,Builder 将开始生产构建。如果我不先构建我的应用,它就无法提供服务还是我错了?
    猜你喜欢
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-24
    相关资源
    最近更新 更多