Nuxt 和 Ag Grid 问题 SyntaxError Missing stack frames

Nuxt and Ag Grid issue SyntaxError Missing stack frames

正在尝试在 nuxt 应用程序中添加 ag-grid。

我按照以下步骤操作 https://www.ag-grid.com/vue-getting-started/

注意:请不要尝试 运行 这些片段,因为我只是用它们来分享我的源代码。

我的nuxt.config.js文件

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

export default {
  mode: 'universal',

  /*
   ** 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' }]
  },

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

  /*
   ** Global CSS
   */
  css: [
    { src: '~assets/bulma-modifications.scss', lang: 'scss' },
    { src: 'font-awesome/scss/font-awesome.scss', lang: 'scss' },
    { src: '~/node_modules/ag-grid-community/dist/styles/ag-grid.css',  lang: 'css' },
    { src: '~/node_modules/ag-grid-community/dist/styles/ag-theme-dark.css',  lang: 'css' }
  ],

  /*
   ** Plugins to load before mounting the App
   */
  plugins: [
    {
      src: '~/plugins/plugin-ag-grid.js',
      ssr: false
    },
    {
      src: '~plugins/plugin-vue-chartjs.js',
      ssr: false
    }
  ],

  /*
   ** Nuxt.js modules
   */
  modules: [
    // Doc: https://axios.nuxtjs.org/usage
    '@nuxtjs/axios',
    // Doc: https://buefy.github.io/#/documentation
    'nuxt-buefy',
    '@nuxtjs/pwa',
    '@nuxtjs/dotenv'
  ],
  /*
   ** Axios module configuration
   */
  axios: {
    // See https://github.com/nuxt-community/axios-module#options
  },

  /*
   ** Build configuration
   */
  build: {
    /*
     ** You can extend webpack config here
     */
    extend(config, ctx) {
      config.resolve.alias['vue'] = 'vue/dist/vue.common'
      // Run ESLint on save
      if (ctx.isDev && ctx.isClient) {
        config.module.rules.push({
          enforce: 'pre',
          test: /\.(js|vue)$/,
          loader: 'eslint-loader',
          exclude: /(node_modules)/
        })
      }
      config.node = {
        fs: 'empty'
      }
    }
  },
  env: {
    baseUrl: process.env.BASE_URL || 'http://localhost:3000'
  }
}

我的插件 plugin-ag-grid.js:

import * as agGridEnterpise from 'ag-grid-enterprise/main'
require('dotenv').config()
agGridEnterpise.LicenseManager.setLicenseKey([process.env.AG_LICENSE_KEY])

我的组件AgGridDemo.vue:

<template>
  <ag-grid-vue
    style="width: 500px; height: 500px;"
    class="ag-theme-balham"
    :columnDefs="columnDefs"
    :rowData="rowData"
  ></ag-grid-vue>
</template>
<script>
import { AgGridVue } from 'ag-grid-vue'

export default {
  name: 'AgGridDemo',
  data() {
    return {
      columnDefs: null,
      rowData: null
    }
  },
  components: {
    AgGridVue
  },
  beforeMount() {
    this.columnDefs = [
      { headerName: 'Make', field: 'make' },
      { headerName: 'Model', field: 'model' },
      { headerName: 'Price', field: 'price' }
    ]

    this.rowData = [
      { make: 'Toyota', model: 'Celica', price: 35000 },
      { make: 'Ford', model: 'Mondeo', price: 32000 },
      { make: 'Porsche', model: 'Boxter', price: 72000 }
    ]
  }
}
</script>

最后是我的页面:

<template>
  <section class="section">
    Welcome to test page
    <aggriddemo></aggriddemo>
  </section>
</template>
<script>

import AgGridDemo from '~/components/AgGridDemo'
export default {
  name: 'IndexPage',
  components: {
    AgGridDemo
  }
}
</script>

我在屏幕上收到错误,但在我的控制台上没有,控制台显示编译成功但在屏幕上我得到:

缺少语法错误

堆栈帧

关于为什么会发生这种情况以及如何解决这个问题的任何想法?

首先,虽然不太可能导致此错误,但模板中的组件应该是烤肉串。 <ag-grid-demo/>。来自 vue docs

您遇到的错误可能是 ssr 问题,尽管您在 nuxt.config.js 中指定了 ssr: false,但这并不总能说明问题。

你能试试这个吗:

<template>
  <section class="section">
    Welcome to test page
    <no-ssr>
      <ag-grid-demo></ag-grid-demo>
    </no-ssr>
  </section>
</template>
    
<script>
let AgGridDemo = {}
if (process.browser) {
  AgGridDemo = require('~/components/AgGridDemo')
}
export default {
  components: {
    'ag-grid-demo': AgGridDemo
  }
}
</script>

此外,顺便说一句,在 nuxt.config.js 中导入插件的现代方法如下。

plugins: [
  '~/plugins/plugin-ag-grid.client.js'
  //Note the .client.js This is shorthand for the following which you can also use
  src: { '~/plugins/plugin-ag-grid.js', mode: client }
]

ssr: false 的使用将在下一个主要版本中弃用。参见 docs

编辑

如果这仍然导致错误,您可能需要将插件添加到 nuxt.config.js 中的 build-transpile。像这样:

build: {
  ...
  transpile: [
    '/plugins',
  ],
}

这将转译您所有的插件,但看您的进展如何。不幸的是 docs 没有给我们很多关于这方面的信息。

如果你不能让它工作,老式的方法是像这样将组件添加到白名单:

//nuxt.config.js
const nodeExternals = require('webpack-node-externals')

module.exports = {
  /**
   * All other config code
   */
  build: {
    extend(config, ctx) {
      if (ctx.isServer) {
        config.externals = [
          nodeExternals({
            whitelist: [/^@components\AgGridDemo.vue/] 
            // or however you regex a windows path
          })
        ]
      }
    }
  }
}

在尝试了此处给出的所有解决方案后(感谢您发帖),我设法在 nuxt 项目 using < no-ssr > tag and dynamic import (minute 7:40 is explained how to do it, if you are not familiar with codesplitting I deeply recommend to watch the whole video) dynamic import video.

上渲染了 ag-grid

我是怎么到那里的?好吧,既然安德鲁写道问题可能与 ssr,I switched my nuxt project into mode: 'spa' 和 BOOM 有关! ag-grid-vue 出现了。现在的问题是,我的许多功能都非常基于 ssr 的东西。所以我不得不让它在 srr 模式下工作,但现在我知道 ag-grid-vue 在客户端完全可用,所以我切换回模式:ssr 并做了以下操作:

注意:请不要尝试 运行 这些片段,因为我只是用它们来分享我的源代码。

我的agGridDemo.vue

<template>
  <ag-grid-vue
    style="width: 500px; height: 500px;"
    class="ag-theme-balham"
    :columnDefs="columnDefs"
    :rowData="rowData"
  ></ag-grid-vue>
</template>
<script>
import { AgGridVue } from 'ag-grid-vue'

export default {
  name: 'ag-grid-demo',
  data() {
    return {
      columnDefs: null,
      rowData: null
    }
  },
  components: {
    AgGridVue
  },
  beforeMount() {
    this.columnDefs = [
      { headerName: 'Make', field: 'make' },
      { headerName: 'Model', field: 'model' },
      { headerName: 'Price', field: 'price' }
    ]

    this.rowData = [
      { make: 'Toyota', model: 'Celica', price: 35000 },
      { make: 'Ford', model: 'Mondeo', price: 32000 },
      { make: 'Porsche', model: 'Boxter', price: 72000 }
    ]
  }
}
</script>

<style lang="scss">
  @import "~/node_modules/ag-grid-community/dist/styles/ag-grid.css";
  @import "~/node_modules/ag-grid-community/dist/styles/ag-theme-balham.css";
</style>

我想这里没有什么新鲜事,只是在 nuxt.config.js 文件中导入样式表,使样式表无法访问(至少在我的情况下)。所以我添加了