【问题标题】:Nuxt and Ag Grid issue SyntaxError Missing stack framesNuxt 和 Ag Grid 问题 SyntaxError Missing stack frames
【发布时间】:2019-09-24 14:57:32
【问题描述】:

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

我按照以下步骤操作 https://www.ag-grid.com/vue-getting-started/How to use ag-grid in a Nuxt app

  • 在 nuxt.config.js 中添加了样式
  • 制作了一个插件并包含在 nuxt.config.js 中
  • 创建了组件 AgGridDemo.vue
  • 在页面 index.vue 中包含组件

注意:请不要尝试运行 sn-ps,因为我只使用它们来分享我拥有的源代码。

我的 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'
  }
}

我的插件插件-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>

我在屏幕上出现错误,但在我的控制台上没有,控制台说编译成功,但在屏幕上我得到:

缺少语法错误

堆栈帧

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

【问题讨论】:

    标签: vue.js ag-grid nuxt.js


    【解决方案1】:

    在尝试了这里给出的每个解决方案之后(感谢发帖),我设法在一个 nuxt 项目using < no-ssr > tag 上渲染 ag-grid 并动态导入(如果你不熟悉,7:40 分钟会解释如何做)我强烈建议观看整个视频)dynamic import video

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

    注意:请不要尝试运行 sn-ps,因为我只使用它们来分享我拥有的源代码。

    我的 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 文件中导入样式表导致样式表无法访问(至少在我的情况下)。所以我添加了

    这就是奇迹发生的地方,动态导入(如您在我推荐的视频中所见)避免在第一次加载时导入组件代码,并且仅在调用时才导入,这对于优化页面加载通常很有用。所以在我的第一次尝试中,我写了一些这样的:

    <template>
      <section>
          <comAgGridDemo v-if="mostrarGrid " ></comAgGridDemo>
      </section>
    </template>
    
    <script lang="ts">
    import { Component, Vue } from "nuxt-property-decorator";
    // import comAgGridDemo from '~/components/comAgGridDemo.vue'
    const comAgGridDemo = () => import('~/components/comAgGridDemo.vue');
    
    @Component({
      components: {
        comAgGridDemo
      }
    })
    export default class extends Vue {
      mostrarGrid: boolean = false;
    
      mounted() {
        this.mostrarGrid = true
      }
    
    }
    </script>
    

    所以 comAgGridDemo 只会在 mostrarGrid 为真时渲染,然后在挂载的钩子中(因为挂载在客户端可用)我设置了 mostrarGrid = true。它已经完成了。

    我走得更远了,因为我不喜欢使用挂载钩子来解决这个问题,所以我将组件 comAgGridDemo 包装在 标签上,删除 v-if,并且 ag-grid 保持渲染得很好。所以 index.vue 文件最终是这样的:

    我的 index.vue

    <template>
      <section>
        <no-ssr>
          <comAgGridDemo ></comAgGridDemo>
        </no-ssr>
      </section>
    </template>
    
    <script lang="ts">
    import { Component, Vue } from "nuxt-property-decorator";
    // import comAgGridDemo from '~/components/comAgGridDemo.vue'
    const comAgGridDemo = () => import('~/components/comAgGridDemo.vue');
    
    @Component({
      components: {
        comAgGridDemo
      }
    })
    export default class extends Vue {
    
    }
    </script>

    就是这样。

    请注意,如果您。如果你使用 标签和常规导入,ag-grid 将失败。如果使用不带 标签的动态导入,ag-grid 将失败。如果您使用“v-if 方式”,则无需使用 标记。

    使用 ag-grid 在 ssr 上有些奇怪。但这就是设法解决它的方法。如果有更好的解决方案,请指教。

    我做了一个 github 仓库:https://github.com/warheartvik/nuxt-ag-grid

    【讨论】:

      【解决方案2】:

      首先,虽然可能不会导致此错误,但您模板中的组件应该是 kebab case。 &lt;ag-grid-demo/&gt;。来自vuedocs

      您遇到的错误可能是 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
                })
              ]
            }
          }
        }
      }
      

      【讨论】:

      • 感谢安德鲁的回复!我认为这解决了它。我现在不再收到语法错误,但是几乎没有其他问题仍然阻止我渲染 ag-grid。对此进行调查,并在我确保解决此问题后立即将您的答案标记为正确!
      • 我现在得到 [Vue 警告]:无法挂载组件:未定义模板或渲染函数。在 ---> 中找到如果我用 import AgGridDemo from '~/components/AgGridDemo' 替换浏览器所在的行并保存,当 dev 自动重新加载发生时网格工作,但如果我手动重新加载页面pc 上的 cntrl+f5 我又得到了:缺少堆栈帧。
      • @Nesha8x8 我添加了一个可能有帮助的编辑。您根本不会使用 IE11 吗?
      • 不,在 Chrome 和勇敢(铬)中测试
      猜你喜欢
      • 2021-01-06
      • 1970-01-01
      • 1970-01-01
      • 2020-05-26
      • 2021-06-14
      • 2020-10-27
      • 2019-11-20
      • 2018-08-17
      • 2019-01-04
      相关资源
      最近更新 更多