简体   繁体   English

在Aurelia中使用Chart.js

[英]use Chart.js in Aurelia

I'd like to use chart.js in an aurelia project, but I'm getting errors. 我想在aurelia项目中使用chart.js,但是我遇到了错误。 How do I add 3rd party node packages to an aurelia app? 如何将第三方节点包添加到aurelia应用程序?

I'm using aurelia-cli, BTW 我正在使用aurelia-cli,BTW

Here's what I've done 这就是我所做的

npm install --save chart.js

In aurelia.json I added the following aurelia.json我添加了以下内容

"dependencies": [
  ...,
  {
    "name": "chart.js",
    "path": "../node_modules/chart.js/dist",
    "main": "Chart.min.js"
  }
]

In app.html I then add the line app.html我然后添加该行

<require from="chart.js"></require>

But, I get the error: 但是,我得到错误:

vendor-bundle.js:1399 Unhandled rejection Error: Load timeout for modules: template-registry-entry!chart.html,text!chart.html

I've tried various things like injecting the Chart into the app.html 我尝试了各种各样的事情,比如将图表注入app.html

// DIDN'T WORK :-(
// app.js
import {inject} from 'aurelia-framework';
import {Chart} from 'chart.js';

export class App {

  static inject() { return [Chart]};

  constructor() {
    this.message = 'Hello World!';
  }
}

And, then, in app.html, I added the following require statement 然后,在app.html中,我添加了以下require语句

<require from="Chart"></require>

HERE'S THE SOLUTION 这是解决方案

You can checkout a working example here . 你可以在这里查看一个工作示例。 Initially, I thought you had to use the aurelia-chart module, however, it's very difficult to use, and so, I'd recommend you just use Chart.JS package instead. 最初,我认为你必须使用aurelia-chart模块,但是,它很难使用,因此,我建议你只使用Chart.JS包。 Here's how to incorporate the chart.js module into your Aurelia app: 以下是将chart.js模块合并到Aurelia应用程序中的方法:

npm install --save chart.js

In aurelia.json add the following line to the prepend section aurelia.json添加到前置部分

"prepend": [
  ...,
  "node_modules/chart.js/dist/Chart.min.js"
],

In the app.js file (or any other model-view file), add the line app.js文件(或任何其他模型视图文件)中,添加该行

import {Chart} from 'node_modules/chart.js/dist/Chart.js';

For, example, if you wanted to display a chart on the home page: 例如,如果要在主页上显示图表:

// app.js
import {Chart} from 'node_modules/chart.js/dist/Chart.js';

export class App {
  ...
}

And that's it! 就是这样!

1. Problem with require 1.要求问题

First of all, don't use <require from="Chart"></require> in your app.html project. 首先,不要在app.html项目中使用<require from="Chart"></require> That is the source of your error message, since it's trying to load an Aurelia module and chart.js is not an Aurelia module (view/viewmodel) in your source code. 这是您的错误消息的来源,因为它正在尝试加载Aurelia模块,而chart.js不是源代码中的Aurelia模块(view / viewmodel)。

2. Alternate import syntax 2.备用导入语法

Skip the inject lines in app.js , but try one of the following (try them one at a time) in either app.js or in each module you'll be using Chart. 跳过inject的线app.js ,但请尝试以下(试戴一次一个)中任一个 app.js或将要使用图表每个模块中。 One of these imports is likely to work. 其中一种进口可能有效。

import { Chart } from 'chart.js';
import * from 'chart.js';
import 'chart.js';

3. Legacy prepend 3.遗产前置

If none of the above works, import it as a legacy repo using the prepend section of aurelia.json (before the dependencies section) like this: 如果以上都aurelia.json使用aurelia.jsonprepend部分(在dependencies部分之前)将其作为旧的repo导入,如下所示:

"prepend": [
  // probably a couple other things already listed here...
  "node_modules/chart.js/dist/Chart.min.js"
],

Update for Aurelia-Chart: (added for any later viewers) Aurelia-Chart的更新:(为以后的观众添加)

Since you ended up going with aurelia-chart (by grofit), here's the dependency code for aurelia.json : 既然你最终使用了aurelia-chart(通过grofit),这里是aurelia.json的依赖代码:

"dependencies": [
  ...,
  {
    "name": "chart.js",
    "path": "../node_modules/chart.js/dist",
    "main": "Chart.min.js"
  },
  {
    "name": "aurelia-chart",
    "path": "../node_modules/aurelia-chart/dist/amd",
    "main": "index",
    "deps": ["chart.js"]
  }
]

I just got this working with an aurelia cli project and it required some extra modifications. 我刚刚使用了aurelia cli项目,需要进行一些额外的修改。

I used au install chart.js but there is an open issue that states it is not intelligent enough yet to add references to package dependencies. 我使用了au install chart.js但是有一个未解决的问题 ,即它还没有足够的智能来添加对包依赖的引用。

To make things work I added the following to my aurelia.json dependencies: 为了使事情有效,我将以下内容添加到我的aurelia.json依赖项中:

"moment",
"chartjs-color",
"chartjs-color-string",
{
  "name": "chart.js",
  "main": "src/chart.js",
  "path": "../node_modules/chart.js",
  "deps": ["chartjs-color", "moment"]
},
{
  "name": "color-convert",
  "path": "../node_modules/color-convert",
  "main": "index"
},
{
  "name": "color-name",
  "path": "../node_modules/color-name",
  "main": "index"
}

I was then able to import { Chart } from 'chart.js'; 然后我可以import { Chart } from 'chart.js'; in my view model and run the chart.js quick start example from the attached viewmodel lifecycle method. 在我的视图模型中,从attached viewmodel生命周期方法运行chart.js快速启动示例。

In the chart.js docs they mention including the minified version can cause issues if your project already depends on the moment library. 在chart.js文档中,他们提到包括缩小版本可能会导致问题,如果您的项目已经依赖于时刻库。

The bundled version includes Moment.js built into the same file. 捆绑版本包含内置于同一文件中的Moment.js。 This version should be used if you wish to use time axes and want a single file to include. 如果您希望使用时间轴并希望包含单个文件,则应使用此版本。 Do not use this build if your application already includes Moment.js. 如果您的应用程序已包含Moment.js,请不要使用此版本。 If you do, Moment.js will be included twice, increasing the page load time and potentially introducing version issues. 如果这样做,Moment.js将被包含两次,增加页面加载时间并可能引入版本问题。

This solution may help if you are in that position. 如果您处于该位置,此解决方案可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM