2017-01-31 10 views
0

私はaurelia、chart.js、moment.jsに問題があります。Aurelia - 未処理の拒否 - Chart.jsとMoment.js

実際、他のクラスではmoment.jsとchart.jsを別々に使用しています。私はこのエラーを取得するXスケールで時間dimesionとのプロットを描画するためのchart.js時間スケールの機能を使用しようとした場合、それにもかかわらず:

[Warning] Unhandled rejection Error: Chart.js - Moment.js could not be found! You must include it before Chart.js to use the time scale. Download at https://momentjs.com (vendor-bundle.js, line 1395) 
    [email protected]://localhost:9001/scripts/vendor-bundle.js:35970:13268 
    [email protected]://localhost:9001/scripts/vendor-bundle.js:35968:6217 
    [email protected]://localhost:9001/scripts/vendor-bundle.js:35968:7779 
    [email protected]://localhost:9001/scripts/vendor-bundle.js:35967:28931 
    [email protected]://localhost:9001/scripts/vendor-bundle.js:35967:27712 
    [email protected]://localhost:9001/scripts/vendor-bundle.js:35967:27429 
    [email protected]://localhost:9001/scripts/vendor-bundle.js:35968:22824 
    [email protected]://localhost:9001/scripts/app-bundle.js:1155:22 
    [email protected]://localhost:9001/scripts/app-bundle.js:1160:22 
    [email protected]://localhost:9001/scripts/vendor-bundle.js:21894:32 
    [email protected]://localhost:9001/scripts/vendor-bundle.js:19969:32 
    [email protected]://localhost:9001/scripts/vendor-bundle.js:20323:23 
    [email protected]://localhost:9001/scripts/vendor-bundle.js:19974:29 
    [email protected]://localhost:9001/scripts/vendor-bundle.js:20323:23 
From previous event: 
    [email protected]://localhost:9001/scripts/app-bundle.js:579:29 
From previous event: 
    [email protected][native code] 
From previous event: 
    [email protected]://localhost:9001/scripts/vendor-bundle.js:5432:38 
    [email protected]://localhost:9001/scripts/vendor-bundle.js:4620:57 
    [email protected]://localhost:9001/scripts/vendor-bundle.js:4912:27 
    [email protected]://localhost:9001/scripts/vendor-bundle.js:5293:45 
    [email protected]://localhost:9001/scripts/vendor-bundle.js:3798:35 
    [email protected]://localhost:9001/scripts/vendor-bundle.js:4849:21 
    [email protected]://localhost:9001/scripts/vendor-bundle.js:4525:32 

私はaurelia.jsonファイルにLIBSを追加しました。.. ..

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

${value}が実際にモーメント文字列に置き換えられますことを...

注このテンプレート・コンポーネントを作成しました。だから、moment.js ...

<template> 

    <div class="panel panel-default"> 
    <div class="panel-heading"> 
     <h3 class="panel-title">${value}</h3> 
    </div> 
    <div class="panel-body"> 

     <canvas id="mychart"/> 

    </div> 
    </div> 

</template> 

をここから入手可能であり、このtypescriptですクラス書きました:あなたのサポートのために、事前に

import {autoinject} from 'aurelia-framework'; 
import {SalesService} from "../../service/SalesService"; 
import {EventAggregator} from "aurelia-event-aggregator"; 
import {Subscription} from "aurelia-event-aggregator"; 
import {FilterService} from "../../service/FilterService"; 
import {SalesPeriodSum} from "../../domain/SalesPeriodSum"; 
import * as moment from 'moment'; 
import Moment = moment.Moment; 
import * as Chart from 'chart.js'; 

@autoinject 
export class SalesInPeriod { 

    public value: Moment; 
    private _salesService: SalesService; 
    private _eventAggregator: EventAggregator; 
    private _subscription: Subscription; 
    private _filterService: FilterService; 
    private _items: Array<SalesPeriodSum>; 

    constructor(salesService: SalesService, eventAggregator: EventAggregator, filterService: FilterService) { 
    this._salesService = salesService; 
    this._eventAggregator = eventAggregator; 
    this._filterService = filterService; 
    } 

    private load() { 
    this._salesService.getSalesInPeriod(this._filterService.request).then(result => { 
     this._items = result; 
    }); 

    this.value = moment(); 

    let config = { 
     type: 'line', 
     data: { 
     datasets: [{ 
      label: "Dataset with string point data", 
      data: [{ 
      x: moment().add(0, 'days'), 
      y: 100 
      }, { 
      x: moment().add(1, 'days'), 
      y: 120 
      }, { 
      x: moment().add(2, 'days'), 
      y: 90 
      }, { 
      x: moment().add(3, 'days'), 
      y: 105 
      }], 
      fill: false 
     }] 
     }, 
     options: { 
     responsive: true, 
     title: { 
      display: true, 
      text: "Chart.js Time Point Data" 
     }, 
     scales: { 
      xAxes: [{ 
      type: "time", 
      display: true, 
      scaleLabel: { 
       display: true, 
       labelString: 'Date' 
      } 
      }], 
      yAxes: [{ 
      display: true, 
      scaleLabel: { 
       display: true, 
       labelString: 'value' 
      } 
      }] 
     } 
     } 
    }; 

    let ctx = document.getElementById("mychart"); 
    new Chart(ctx, config); 
    } 

    attached() { 
    this._subscription = this._eventAggregator.subscribe('filter_changed', response => this.load()); 
    this.load(); 
    } 

    detached() { 
    this._subscription.dispose(); 
    } 

    get items(): Array <SalesPeriodSum> { 
    return this._items; 
    } 

} 

感謝を。

答えて

0

Moment.jsグローバル変数として設定し、Chart.jsのタイムスケールが機能するようにします。

import { Moment } from 'moment'; 
window.Moment = Moment; 

を行うと、それを修正する必要があります。

+0

感謝。 chart.bundle.min.jsファイルを読み込んで解決策を見つけました。このスクリプトにはmoment.jsが含まれています。しかし、実際それは両方が欲しいものではありません。だから、自分の変数をウィンドウに追加する唯一の方法は、次のステートメントを使うことです。 (ウィンドウ).Moment = moment; '。その後、私はコンソール上でモーメントを見ることができますが、上記と同じエラーが表示されます。 – Fritz

+0

@Fritz、それは**瞬間**を探しているからかもしれません。**モーメント**はウィンドウ上にあります: https://github.com/chartjs/Chart.js/blob/master/src/scales/scale .time.js#L5 https://github.com/chartjs/Chart.js/blob/master/src/scales/scale.time.js#L375 – Patrick

0
次へ aurelia.jsonであなたの瞬間の定義を変更し

:あなたの答えのための

{ 
    "name": "moment", 
    "path": "../node_modules/moment", 
    "main": "moment" 
}, 
+0

これで解決しましたか? – LStarky

関連する問題