【问题标题】:SystemJS import chart.js in index.htmlSystemJS 在 index.html 中导入 chart.js
【发布时间】:2016-08-10 04:20:03
【问题描述】:

我是 angularjs 的新手(特别是 angular js2)。

我对 SystemJS 导入/配置感到困惑。

我的应用程序 (angularjs2) 可以正常工作。我正在使用 Visual Studio 开发它。

我已经设置了在 node_modules 中加载我必要的东西的 package.json。 就我而言,我想使用 chart.js

来自 package.json 文件的

sn-p

"dependencies": {
    "angular2": "2.0.0-beta.15",
    "es6-shim": "^0.35.0",
    "reflect-metadata": "0.1.2",
    "rxjs": "5.0.0-beta.2",
    "systemjs": "^0.19.26",
    "zone.js": "0.6.11",
    "bootstrap": "3.3.6",
    "jquery": "2.2.3",
    "font-awesome": "4.5.0",
    "toastr": "2.1.2",
    "chart.js": "2.0.2"    
  },

在我的 Index.html 中,我有以下内容:

<!-- 2. Configure SystemJS -->
    <script>
        System.config({
            packages: {
                app: {
                    format: 'register',
                    defaultExtension: 'js'
                }
            }
        });

        System.import('app/main')
              .then(null, console.error.bind(console));

        $(document).ready(function () {
            window.toastr = toastr;
            setTimeout(function () {
                toastr.options = {
                    closeButton: true,
                    progressBar: true,
                    showMethod: 'slideDown',
                    timeOut: 2500
                };
                toastr.success('Welcome to the APP');

            }, 1300);
        });
    </script>

Chart JS如何导入:

 <!-- ChartJS-->
 <!--version1: OLD ChartJS importing WORKS ok--> 
 <!--<script src="scripts/plugins/chartJs/Chart.min.js"></script>-->

 <!--version2: This kind of importing does not work-->
 <script src="node_modules/chart.js/dist/Chart.min.js"></script>

注意:

我有一个 toastr,它也是通过这种方式从 node_modules 导入的:

   <!-- Toastr -->
    <script src="node_modules/toastr/build/toastr.min.js"></script>

但它工作正常。

node_modules 文件夹不包含在我的解决方案中

我将在组件中使用它(稍后在单独的指令中 - 根据最佳方法)

在那一行

var myNewChart = new Chart(ctx).Line(lineData, options);

它在抱怨:

ReferenceError: Chart is not defined
    at DashboardComponent.execute.DashboardComponent.createLinearChart

【问题讨论】:

    标签: visual-studio angular node-modules systemjs


    【解决方案1】:

    我会以这种方式在 SystemJS 中配置该库,因为该库支持 CommonJS:

    System.config({
      map: {
        chart: 'node_modules/chart.js/dist/Chart.js'
      },
      packages: {
        (...)
      }
    });
    

    这样您就可以导入Chart 对象:

    import Chart from 'chart';
    
    (...)
    var myNewChart = new Chart(...);
    

    【讨论】:

    • 我是这样设置的:map: { 'chart': 'node_modules/chart.js/dist/Chart.js' } 在我的dashboard.component.ts 中抱怨import Chart from 'chart'; 在抱怨找不到模块'chart'
    • 在浏览器控制台中:很多GET http://localhost:3000/node_modules/chart.js/dist/charts/Chart.PolarArea 404 (Not Found)GET http://localhost:3000/chartjs-color 404 (Not Found)GET http://localhost:3000/node_modules/chart.js/dist/charts/Chart.Bar 404 (Not Found)
    • 这有点奇怪,因为 chart.js 依赖于不在 dist 文件夹中的 JS 文件(使用 require(...) 加载):-(
    • 您可以尝试将其添加到 map 块中:'chart/charts': 'node_modules/chart.js/src/charts。只是猜测;-)
    【解决方案2】:

    Chart.js2.2.1 版本中发布并且 Angular 2RC4 之后是可以通过以下方式使用 chart.js

    //package.json
    "dependencies": {
        "@angular/common": "2.0.0-rc.4",
        "@angular/compiler": "2.0.0-rc.4",
        "@angular/core": "2.0.0-rc.4",
        "@angular/forms": "0.2.0",
        "@angular/http": "2.0.0-rc.4",
        "@angular/platform-browser": "2.0.0-rc.4",
        "@angular/platform-browser-dynamic": "2.0.0-rc.4",
        "@angular/router": "3.0.0-beta.2",
        "bootstrap": "3.3.7",
    
        "chart.js": "2.2.1",
        "...ETC"
    

    从 node_modules 导入:

    //Index.html
    <script src="node_modules/chart.js/dist/Chart.js"></script>
    

    实际使用:

    this.chart = new Chart(ctx, { type: 'line', data: data, options: chartOptions });
    

    有关“如何使用”的更多详细信息,请参阅此帖子:chart.js - Supplied parameters do not match any signature of call target (angular2)

    【讨论】:

    • 有没有办法从 index.html 中删除该行并将其放入 systemjs.config.js 文件中?
    • @reza,无法回答。我切换到 webpack,我正在使用供应商文件来注入 3rd 方库。也许蒂埃里的回答对你来说还可以?
    猜你喜欢
    • 1970-01-01
    • 2016-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    • 2017-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多