尝试在 nuxt 应用程序中添加 ag-grid。
我按照 https://www.ag-grid.com/vue-getting-started/ 和 如何在 Nuxt 应用程序中使用 ag-grid 中的步骤操作
- 在 nuxt.config.js 中添加样式
- 制作了一个插件并包含在 nuxt.config.js 中
- 创建组件 AgGridDemo.vue
- 在页面 index.vue 中包含组件
注意:请不要尝试运行这些片段,因为我只是用它们来分享我拥有的源代码。
我的 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>
我在屏幕上收到错误,但在我的控制台上没有,控制台显示编译成功但在屏幕上我得到:
缺少语法错误
栈帧
关于为什么会发生这种情况以及如何解决这个问题的任何想法?
原文由 nkostic 发布,翻译遵循 CC BY-SA 4.0 许可协议
在尝试了这里给出的每个解决方案之后(感谢发布),我设法 使用 标签 和动态导入在 nuxt 项目上渲染 ag-grid(如果你不喜欢,第 7:40 分钟解释了如何做)熟悉codesplitting我强烈推荐观看整个视频) 动态导入视频。
我是怎么到那里的?好吧,由于安德鲁写道问题可能与 ssr 有关, 我将我的 nuxt 项目切换到模式:’spa’ 和 BOOM! ag-grid-vue 出现了。现在的问题是,我的许多功能都非常基于 ssr 的东西。所以我不得不让它在 srr 模式下工作,但现在我知道 ag-grid-vue 在客户端完全可用,所以我切换回模式:ssr 并做了以下内容:
注意:请不要尝试运行这些片段,因为我只是用它们来分享我拥有的源代码。
我的 agGridDemo.vue
我想这里没有什么新鲜事,只是在 nuxt.config.js 文件中导入样式表,使样式表无法访问(至少在我的情况下)。所以我添加了