在angular2项目上使用ng2-charts显示angular2-polyfill的错误

时间:2016-02-26 16:42:11

标签: charts angular

当我尝试使用基于http://valor-software.com/ng2-charts/的ng2-charts并使用5分钟从angular2开始时,我得到以下错误,指导我不在哪里:

SyntaxError: Unexpected token <(…)Zone.run @ angular2-polyfills.js:1243
angular2-polyfills.js:1152 DEPRECATION WARNING: 'dequeueTask' is no
longer supported and will be removed in next major release. Use 
removeTask/removeRepeatingTask/removeMicroTask

项目运行正常,package.json是:

{
  "name": "angular2-quickstart",
  "version": "1.0.0",
  "scripts": {
    "start": "concurrent \"npm run tsc:w\" \"npm run lite\" ",
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "lite": "lite-server",
    "typings": "typings",
    "postinstall": "typings install"
  },
  "license": "ISC",
  "dependencies": {
    "angular2": "2.0.0-beta.7",
    "systemjs": "0.19.22",
    "es6-promise": "^3.0.2",
    "es6-shim": "^0.33.3",
    "reflect-metadata": "0.1.2",
    "rxjs": "5.0.0-beta.2",
    "zone.js": "0.5.15",
    "ng2-charts": "~1.0.0-beta.0"
  },
  "devDependencies": {
    "concurrently": "^2.0.0",
    "lite-server": "^2.1.0",
    "typescript": "^1.7.5",
    "typings": "^0.6.8"
  }
}

app.component.ts

import {Component, } from 'angular2/core';
import {JSONP_PROVIDERS}  from 'angular2/http';
import {ConsumerService} from './consumers/url.consumer.service';
import {Observable} from "rxjs/Observable";
import {HTTP_PROVIDERS} from "angular2/http";
import {Hero} from "./Billing"
import {CHART_DIRECTIVES} from 'ng2-charts/ng2-charts';
import {CORE_DIRECTIVES, FORM_DIRECTIVES, NgClass} from 'angular2/common';
// webpack html imports
let template = require('./line-chart-demo.html');

@Component({
    selector: 'my-app',
    template: template,
    providers:[HTTP_PROVIDERS, JSONP_PROVIDERS, ConsumerService, CHART_DIRECTIVES, NgClass, CORE_DIRECTIVES, FORM_DIRECTIVES]
})
export class AppComponent {

    constructor (private _consumerService: ConsumerService) {}

    items: Observable<Hero[]>;

    search (term: String) {
    this.items = this._consumerService.getHeroes();
    }


    // lineChart
    private lineChartData:Array<any> = [
    [65, 59, 80, 81, 56, 55, 40],
    [28, 48, 40, 19, 86, 27, 90],
    [18, 48, 77, 9, 100, 27, 40]
    ];
    private lineChartLabels:Array<any> = ['January', 'February', 'March', 'April', 'May', 'June', 'July'];
    private lineChartSeries:Array<any> = ['Series A', 'Series B', 'Series C'];
    private lineChartOptions:any = {
    animation: false,
    responsive: true,
    multiTooltipTemplate: '<%if (datasetLabel){%><%=datasetLabel %>: <%}%><%= value %>'
    };
    private lineChartColours:Array<any> = [
    { // grey
        fillColor: 'rgba(148,159,177,0.2)',
        strokeColor: 'rgba(148,159,177,1)',
        pointColor: 'rgba(148,159,177,1)',
        pointStrokeColor: '#fff',
        pointHighlightFill: '#fff',
        pointHighlightStroke: 'rgba(148,159,177,0.8)'
    },
    { // dark grey
        fillColor: 'rgba(77,83,96,0.2)',
        strokeColor: 'rgba(77,83,96,1)',
        pointColor: 'rgba(77,83,96,1)',
        pointStrokeColor: '#fff',
        pointHighlightFill: '#fff',
        pointHighlightStroke: 'rgba(77,83,96,1)'
    },
    { // grey
        fillColor: 'rgba(148,159,177,0.2)',
        strokeColor: 'rgba(148,159,177,1)',
        pointColor: 'rgba(148,159,177,1)',
        pointStrokeColor: '#fff',
        pointHighlightFill: '#fff',
        pointHighlightStroke: 'rgba(148,159,177,0.8)'
    }
    ];
    private lineChartLegend:boolean = true;
    private lineChartType:string = 'Line';

    private randomize() {
    let _lineChartData = [];
    for (let i = 0; i < this.lineChartData.length; i++) {
        _lineChartData[i] = [];
        for (let j = 0; j < this.lineChartData[i].length; j++) {
            _lineChartData[i].push(Math.floor((Math.random() * 100) + 1));

        }
    }
    this.lineChartData = _lineChartData;
    }

    // events
    chartClicked(e:any) {
    console.log(e);
    }

    chartHovered(e:any) {
    console.log(e);
    }

}

的index.html

<html>
  <head>
    <title>Angular 2 QuickStart</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">    

    <!-- 1. Load libraries -->
    <!-- IE required polyfills, in this exact order -->
    <script src="node_modules/es6-shim/es6-shim.min.js"></script>
    <script src="node_modules/systemjs/dist/system-polyfills.js"></script>

    <script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <script src="node_modules/rxjs/bundles/Rx.js"></script>
    <script src="node_modules/angular2/bundles/angular2.dev.js"></script>

    <!-- http calls -->
    <script src="node_modules/angular2/bundles/http.dev.js"></script>

    <!-- charts -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.min.js"></script>

    <!-- 2. Configure SystemJS -->
    <script>
      System.config({
    map: {
      'ng2-charts': 'node_modules/ng2-charts'
    },
    packages: {
      app: {
        format: 'register',
        defaultExtension: 'js'
      }
    }
      });
      System.import('app/main')
        .then(null, console.error.bind(console));
    </script>

  </head>

  <!-- 3. Display the application -->
  <body>
    <my-app>Loading...</my-app>
  </body>

</html>

polyfills.js

1236=>    Zone.prototype.run = function (fn, applyTo, applyWith) {
        applyWith = applyWith || [];
        var oldZone = global.zone;
        // MAKE THIS ZONE THE CURRENT ZONE
        global.zone = this;
        try {
            this.beforeTask();
            return fn.apply(applyTo, applyWith);
        }
        catch (e) {
            if (this.onError) {
                this.onError(e);
            }
            else {
                throw e;
            }
        }
        finally {
            this.afterTask();
            // REVERT THE CURRENT ZONE BACK TO THE ORIGINAL ZONE
            global.zone = oldZone;
        }
    };
在polyfills断点中

applyWith:

&#34;意外的令牌&lt;评估http://localhost:3000/ng2-charts/ng2-charts加载http://localhost:3000/app/main.js&#34;

时出错

main.js:

System.register(['angular2/platform/browser', './app.component', 'rxjs/Rx'], function(exports_1, context_1) {
    "use strict";
    var __moduleName = context_1 && context_1.id;
    var browser_1, app_component_1;
    return {
    setters:[
        function (browser_1_1) {
            browser_1 = browser_1_1;
        },
        function (app_component_1_1) {
            app_component_1 = app_component_1_1;
        },
        function (_1) {}],
    execute: function() {
        browser_1.bootstrap(app_component_1.AppComponent);
    }
    }
});
//# sourceMappingURL=main.js.map

main.ts:

import {bootstrap}    from 'angular2/platform/browser'
import {AppComponent} from './app.component'
import 'rxjs/Rx';

bootstrap(AppComponent);

2 个答案:

答案 0 :(得分:5)

如果你使用SystemJS和npm,你应该像这样配置。

  System.config({
    packages: {        
      app: {
        format: 'register',
        defaultExtension: 'js'
      },
      'ng2-charts': {
        defaultExtension: 'js'
      }
    },
    map: {
      'ng2-charts': 'node_modules/ng2-charts'
    }
  });
  System.import('app/main')
        .then(null, console.error.bind(console));

答案 1 :(得分:0)

您需要在SystemJS配置中添加地图条目:

   $query = "
    INSERT INTO table1 (team_id, user_id, team_leader, start_date)
    SELECT b.team_id,
           a.user_id,
           IF (a.agent_id = :leader, 1, 0),
           :startdate
    FROM table2 a
    LEFT JOIN table3 b ON a.ccb_team_id = b.ccb_team_id
    WHERE b.team_active = 1
      AND a.user_active = 1
      AND b.metro_id = :metroid 
      AND a.ccb_team_id = :teamid
    ON DUPLICATE KEY
       UPDATE team_leader = VALUES(team_leader)";