ag-Grid 中的警告:您正在混合模块(即@ag-grid-community/core)和包(ag-grid-community)

Warning in ag-Grid: You are mixing modules (i.e. @ag-grid-community/core) and packages (ag-grid-community)

我正在为 ag-grid 使用这些 npm 模块:

"@ag-grid-community/react": "^25.0.1",
"@ag-grid-enterprise/all-modules": "25.0.1",
"ag-grid-community": "^25.0.1",
"ag-grid-enterprise": "^25.0.1",
"ag-grid-react": "^25.0.1",

和这些导入:

  import React, { Component } from 'react';
  import ReactDOM from 'react-dom';
  import { AgGridReact } from 'ag-grid-react';
  import 'ag-grid-enterprise';
  import { ModuleRegistry, AllModules } from '@ag-grid-enterprise/all-modules';
  import '@ag-grid-community/all-modules/dist/styles/ag-grid.css';
  import '@ag-grid-community/all-modules/dist/styles/ag-theme-material.css';
  import { LicenseManager } from '@ag-grid-enterprise/core';
    
  LicenseManager.setLicenseKey(
      'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  );
  ModuleRegistry.registerModules(AllModules);

为什么我会在控制台中收到此警告以及如何摆脱它?

ag-Grid: You are mixing modules (i.e. @ag-grid-community/core) and packages (ag-grid-community) - you can only use one or the other of these mechanisms.

您收到警告的原因很简单,您正在混合使用模块和包。库 ag-grid-community 包含 ag-Grid 社区中的所有内容,而 @ag-grid-community/core 包含来自 ag-Grid 社区的核心项目。您需要安装一个或另一个,具体取决于您使用的是模块还是包。

来自农业网格:

There are two main ways to install ag-Grid - either by using packages , or by using modules. packages are the easiest way to use ag-Grid, but by default include all code specific to each package, whereas modules allow you to cherry pick what functionality you want, which will allow for a reduced overall bundle size.

查看有关包和模块的文档 here

您只需要在 package.json 文件中包含以下内容,因为这些包包含您遵循 AG Grid 'package' 方法所需的所有代码。

"ag-grid-community": "^25.0.1",
"ag-grid-enterprise": "^25.0.1",
"ag-grid-react": "^25.0.1",

来自docs

It is important that you do not mix packages and modules in the same application as this will result in AG Grid being included twice and doubling your bundle size! All modules are scoped by either @ag-grid-community/* or @ag-grid-enterprise/* and should not be mixed with the standalone packages of ag-grid-community and ag-grid-enterprise.

Modules Packages
@ag-grid-community/xxxxx ag-grid-community
@ag-grid-enterprise/xxxxx ag-grid-enterprise

我在这个 blog post 中写了更多关于这个的内容。