【问题标题】:CanvasJS with Angular 11: You may need an additional loader to handle the result of these loadersCanvasJS with Angular 11:您可能需要一个额外的加载器来处理这些加载器的结果
【发布时间】:2021-07-21 17:08:25
【问题描述】:

我尝试在 Angular 11 项目中使用 CanvasJS,但导入时出错:

Failed to compile.

./node_modules/canvasjs/src/charts/index.js 1:7
Module parse failed: Unexpected token (1:7)
File was processed with these loaders:
 * ./node_modules/@angular-devkit/build-angular/src/babel/webpack-loader.js
 * ./node_modules/@ngtools/webpack/src/ivy/index.js
You may need an additional loader to handle the result of these loaders.
> export SplineChart from '../charts/spline';
| export ColumnChart from '../charts/column';
| export StackedColumnChart from '../charts/stacked_column';

package.json:

"dependencies": {
  ...
  "canvasjs": "^1.8.3",
  ...
},
"devDependencies": {
  ...
  "@types/canvasjs": "^1.9.7",

我在组件中的导入看起来像这样(没有错误):

import * as CanvasJS from 'canvasjs';

我在 ngOnInit() 上初始化图表(示例代码):

ngOnInit(): void {
  let chart = new CanvasJS.Chart("chartContainer", {
    animationEnabled: true,
    exportEnabled: true,
    title: {
      text: "Basic Column Chart in Angular"
    },
    data: [{
      type: "column",
        dataPoints: [
          { y: 71, label: "Apple" },
          { y: 55, label: "Mango" },
          { y: 50, label: "Orange" },
          { y: 65, label: "Banana" },
          { y: 95, label: "Pineapple" },
          { y: 68, label: "Pears" },
          { y: 28, label: "Grapes" },
          { y: 34, label: "Lychee" },
          { y: 14, label: "Jackfruit" }
        ]
    }]
  });
  
  chart.render();
}

我错过了什么?

【问题讨论】:

    标签: angular angular11 canvasjs


    【解决方案1】:

    您似乎在使用过时/非官方版本的 CanvasJS。按照以下步骤在您的 Angular 应用程序中添加 CanvasJS 图表。

    1. 从CanvasJS Download Page下载正式版/从My Account Page下载商业版。

    2. 将 canvasjs.min.js 保存到 app 文件夹中

    3. 导入库

      import * as CanvasJS from './canvasjs.min';
      
    4. 创建和渲染图表

      var chart = new CanvasJS.Chart({
        //Chart Options Goes Here
      });
      chart.render();
      

    查看CanvasJS Angular Gallery 以获取更多可下载并在本地运行的代码/示例示例。 Here is a live example.

    【讨论】:

    • 谢谢。我没有提出解决方案,但这正是我发现的。一开始,我使用 node_modules 中的缩小版 js。
    猜你喜欢
    • 2020-12-04
    • 2020-04-05
    • 1970-01-01
    • 1970-01-01
    • 2022-11-06
    • 2020-09-12
    • 2021-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多