【问题标题】:Getting a 404 error when trying to use ng2-charts尝试使用 ng2-charts 时出现 404 错误
【发布时间】:2017-08-22 11:53:56
【问题描述】:

我在尝试使 ng2-charts 工作时遇到问题。我在 GitHub 和这里阅读了许多类似的帖子,但到目前为止没有任何成功。 错误是:

Error: (SystemJS) XHR error (404 Not Found) loading http://localhost:9823/ng2-charts
    Error: XHR error (404 Not Found) loading http://localhost:9823/ng2-charts
        at XMLHttpRequest.wrapFn [as _onreadystatechange] (http://localhost:9823/lib/zone.js/dist/zone.js:698:29)
        at ZoneDelegate.invokeTask (http://localhost:9823/lib/zone.js/dist/zone.js:265:35)
        at Zone.runTask (http://localhost:9823/lib/zone.js/dist/zone.js:154:47)
        at XMLHttpRequest.ZoneTask.invoke (http://localhost:9823/lib/zone.js/dist/zone.js:335:33)
    Error loading http://localhost:9823/ng2-charts as "ng2-charts" from http://localhost:9823/lib/spa/app.module.js
        at XMLHttpRequest.wrapFn [as _onreadystatechange] (http://localhost:9823/lib/zone.js/dist/zone.js:698:29)
        at ZoneDelegate.invokeTask (http://localhost:9823/lib/zone.js/dist/zone.js:265:35)
        at Zone.runTask (http://localhost:9823/lib/zone.js/dist/zone.js:154:47)
        at XMLHttpRequest.ZoneTask.invoke (http://localhost:9823/lib/zone.js/dist/zone.js:335:33)
    Error loading http://localhost:9823/ng2-charts as "ng2-charts" from http://localhost:9823/lib/spa/app.module.js

我的 app.module.ts:

import {ChartsModule} from 'ng2-charts';
[...]
@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        HttpModule,
        routing,
        AccountModule,
        ChartsModule
    ] [...]

我的系统js.config.js

(function (global) {
    System.config({
        paths: {
            // paths serve as alias
            'npm:': 'lib/'
        },
        // map tells the System loader where to look for things
        map: {
            // our app is within the app folder
            app: 'lib/spa',
            // angular bundles
            '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
            '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
            '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
            '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
            '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
            '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
            '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
            '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
            // other libraries
            'rxjs': 'npm:rxjs',
            'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js',
            'ng2-charts': 'npm:ng2-charts',
        },
        // packages tells the System loader how to load when no filename and/or no extension
        packages: {
            app: {
                main: './main.js',
                defaultExtension: 'js'
            },
            rxjs: {
                defaultExtension: 'js'
            },
            'angular2-in-memory-web-api': {
                main: './index.js',
                defaultExtension: 'js'
            },
            'ng2-charts': {
                defaultExtension: 'js'
            }

        }
    });
})(this);

我对 Angular2 还很陌生,所以我将非常感谢任何有关如何使图表工作的提示。 当然,一开始我是通过 bower 安装 chart.js 和 ng2-charts 的,我包括了

<script src="~/lib/chart.js/dist/Chart.js"></script>

在我的 Index.cshtml 文件中。

【问题讨论】:

    标签: angular ng2-charts


    【解决方案1】:

    ./src/systemjs.config.js 文件的以下配置中的 map 下添加 'ng2-charts': 'npm:ng2-charts/bundles/ng2-charts.umd.min.js' 行。我正在使用 Angular 4.3.4ng2-charts 1.6.0,这对我来说非常适合。

    /**  * System configuration for Angular samples  * Adjust as necessary for your application needs.  */ (function (global) {   System.config({
        paths: {
          // paths serve as alias
          'npm:': 'node_modules/'
        },
        // map tells the System loader where to look for things
        map: {
          // our app is within the app folder
          'app': 'app',
    
          // angular bundles
          '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
          '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
          '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
          '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
          '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
          '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
          '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
          '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
    
          // other libraries
          'rxjs':                      'npm:rxjs',
          'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js',
          'ng2-charts': 'npm:ng2-charts/bundles/ng2-charts.umd.min.js'
        },
        // packages tells the System loader how to load when no filename and/or no extension
        packages: {
          app: {
            defaultExtension: 'js',
            meta: {
              './*.js': {
                loader: 'systemjs-angular-loader.js'
              }
            }
          },
          rxjs: {
            defaultExtension: 'js'
          }
        }   }); })(this);
    

    【讨论】:

      【解决方案2】:

      在 System.config.js 中更改为,

       map: {
       'ng2-charts': 'npm:ng2-charts'
      }
      
      packages: {
        main: 'ng2-charts.js',
        defaultExtension: 'js'
      }
      

      然后将其导入为,

      import { ChartsModule } from 'ng2-charts/ng2-charts';
      

      【讨论】:

      • 不幸的是它没有工作。我更改了“包”和导入中的内容,但仍然收到相同的错误“(SystemJS)XHR错误(404 Not Found)正在加载localhost:9823/ng2-charts/ng2-charts
      • 检查你的node_modules
      • 我不确定我应该检查什么,但我有 chart.js 和 ng2-charts。
      • 打开node_modules文件夹查看
      猜你喜欢
      • 1970-01-01
      • 2012-09-27
      • 1970-01-01
      • 2014-04-30
      • 2016-11-23
      • 2015-07-31
      • 2016-09-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多