【发布时间】: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>
我在屏幕上出现错误,但在我的控制台上没有,控制台说编译成功,但在屏幕上我得到:
缺少语法错误
堆栈帧
关于为什么会发生这种情况以及如何解决这个问题的任何想法?
【问题讨论】: