代码之家  ›  专栏  ›  技术社区  ›  Steve Fitzsimons

Angular 6导入Karma测试中使用的第三方JS文件的正确方法

  •  0
  • Steve Fitzsimons  · 技术社区  · 7 年前

    ChartsModule 哪些参考文献 Chart.js . 我有 图表.js 已安装并已将其导入我的 angular.json

    "scripts": [
      "node_modules/chart.js/dist/Chart.bundle.min.js"
    ]
    

    应用程序都正常工作,并显示图表。但是,我的组件测试出现以下错误: ReferenceError: Chart is not defined

    我看过几篇建议进口的文章图表.js我的组件测试如下:

    import { Chart } from 'chart.js';
    

    这不管用。我的问题是,导入第三方JS库(如图表.js进入6个业力测试

    编辑 我的组件

    import { Component, OnInit, Input } from '@angular/core';
    
    @Component({
      selector: 'app-my-chart',
      templateUrl: './my-chart.component.html',
      styleUrls: ['./my-chartcomponent.scss']
    })
    export class MyChartComponent implements OnInit {
    
      public data: any;
    
      constructor() { }
    
      ngOnInit() {
        this.data = {
          labels: ['Low', 'Medium', 'High'],
          datasets: [
            {
              data: [300, 50, 10],
              backgroundColor: [
                  '#43a047',
                  '#fb8c00',
                  '#e53935'
              ],
              hoverBackgroundColor: [
                  '#66bb6a',
                  '#ffa726',
                  '#ef5350'
              ]
            }]
          };
      }
    }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Steve Fitzsimons    7 年前

    我还在想以前的事 angular-cli.json 模式。在新的 angular.json 这里有一个测试部分,您还需要添加第三方脚本。

        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.spec.json",
            "karmaConfig": "src/karma.conf.js",
            "styles": [
              "styles.scss"
            ],
            "scripts": [
              "node_modules/chart.js/dist/Chart.bundle.min.js"
            ],
            "assets": [
              "src/favicon.ico"
            ]
          }
        },
    
    推荐文章