Angular chart.js“无法在模块外使用import语句”

htzpubme  于 2023-08-05  发布在  Chart.js
关注(0)|答案(1)|浏览(170)

我想使用chart.js,但我有一些问题。在我看来,这个问题块正确导入这个库.
我还加了这个。没有帮助。

"type": "module",

字符串
我尝试了这个,但我认为这是另一个问题"Uncaught SyntaxError: Cannot use import statement outside a module" when importing ECMAScript 6

import { Chart } from 'chart.js/auto'

@Component({
  selector: 'app-line-add-edit',
  templateUrl: './line-add-edit.component.html',
  styleUrls: ['./line-add-edit.component.scss']
})

export class LineAddEditComponent implements OnInit {

  @ViewChild('chartCanvas') private chartCanvasRef!: ElementRef;
  private chartInstance: Chart | undefined;


.html

<div>
    <canvas #chartCanvas
      (mousemove)="onMouseMove($event)"
      (mouseout)="onMouseOut()"
      style="max-width: 600px; margin: 0 auto;"
    ></canvas>
</div>


.error

Uncaught SyntaxError: Cannot use import statement outside a module (at scripts.js:7:1)


的数据
tsconfig

/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "noImplicitOverride": true,
    "noPropertyAccessFromIndexSignature": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "ES2022",
    "module": "ES2022",
    "useDefineForClassFields": false,
    "lib": [
      "ES2022",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "enableI18nLegacyMessageIdFormat": false,
    "strictInjectionParameters": true,
    "strictInputAccessModifiers": true,
    "strictTemplates": true
  }
}


package.json

{
  "name": "dupa",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "watch": "ng build --watch --configuration development",
    "test": "ng test"
  },
  "type": "module",
  "private": true,
  "dependencies": {
    "@angular/animations": "^16.1.5",
    "@angular/cdk": "^16.1.5",
    "@angular/common": "^16.1.0",
    "@angular/compiler": "^16.1.0",
    "@angular/core": "^16.1.0",
    "@angular/forms": "^16.1.0",
    "@angular/material": "^16.1.5",
    "@angular/platform-browser": "^16.1.0",
    "@angular/platform-browser-dynamic": "^16.1.0",
    "@angular/router": "^16.1.0",
    "@material/form-field": "^14.0.0",
    "@material/grid-list": "^4.0.0",
    "@material/theme": "^14.0.0",
    "@material/toolbar": "^2.3.0",
    "@types/chart.js": "^2.9.37",
    "chart.js": "^4.3.1",
    "material-icons": "^1.13.8",
    "rxjs": "~7.8.0",
    "tslib": "^2.3.0",
    "zone.js": "~0.13.0"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^16.1.4",
    "@angular/cli": "~16.1.4",
    "@angular/compiler-cli": "^16.1.0",
    "@types/jasmine": "~4.3.0",
    "jasmine-core": "~4.6.0",
    "karma": "~6.4.0",
    "karma-chrome-launcher": "~3.2.0",
    "karma-coverage": "~2.2.0",
    "karma-jasmine": "~5.1.0",
    "karma-jasmine-html-reporter": "~2.1.0",
    "typescript": "~5.1.3"
  }
}

j5fpnvbx

j5fpnvbx1#

ok.将chart.js降级到3.9版本解决问题

npm uninstall chart.js
npm install chart.js@3.9.0

字符串

相关问题