404尝试在Angular 5中加载语言环境时出错

时间:2018-03-04 15:08:38

标签: angular

我正在尝试从VS 2015中运行的Angular 5项目中加载一个区域设置。阅读文档,我看到为了添加区域设置,我应该在app.module中添加以下内容:

import { NgModule, LOCALE_ID } from '@angular/core';

...

import { registerLocaleData } from '@angular/common';
import localeFr from '@angular/common/locales/fr';

registerLocaleData(localeFr);

不幸的是,在加载项目时出现错误:

Error: Fetch error: 404 
  Instantiating https://localhost:44334/node_modules/@angular/common/bundles/common.umd.js/locales/fr
  Loading https://localhost:44334/app/app.module.js
  Loading app/main.js
    at fetch.js:37
    at ZoneDelegate.invoke (zone.js:388)
    at Zone.run (zone.js:138)
    at zone.js:858
    at ZoneDelegate.invokeTask (zone.js:421)
    at Zone.runTask (zone.js:188)
    at drainMicroTaskQueue (zone.js:595)
    at <anonymous>

让我感到困惑的是,当我从locales文件夹中请求它时,它会尝试在bundles文件夹下找到该语言环境。

修改

这是我的systemjs.config.js文件。

/**
 * 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',
      '@angular/common/http': 'npm:@angular/common/bundles/common-http.umd.js',
      '@angular/locale/el': 'npm:@angular/common/locales/el.js',
      // other libraries
      'rxjs':                      'npm:rxjs',
      'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js',
      'tslib': 'npm:tslib/tslib.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);

1 个答案:

答案 0 :(得分:1)

使用angular package中的语言环境需要SystemJS的其他配置,因为它们不包含在主包中,不具有专用包。

它们也作为ES模块提供,必须进行转换

SystemJS 0.19.x

简单版本:

System.config({
  paths: {
    'npm:': 'node_modules/'
  },

  transpiler: 'typescript', // we need this to transpile locales

  map: {
    ...
    '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
    '@angular/common/locales': 'npm:@angular/common/locales',
    ...
    'typescript': 'https://unpkg.com/typescript@2.2.1/lib/typescript.js'
                                               ^^^^^^^
                                              required
  },
  packages: {
    ...
    '@angular/common/locales': {
      defaultExtension: 'js'
    },
  }
}

Plunker (我也在本地测试过)

如果您不想使用typescript@2.2.1(而不是&gt; 2.2.1)并从unpkg.com获取,那么请考虑以下配置。

稍微复杂的版本0.19.x:

System.config({
  paths: {
    'npm:': 'node_modules/'
  },

  transpiler: 'typescript',

  map: {
    ...
    '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
    '@angular/common/locales': 'npm:@angular/common/locales',
    ...
    'typescript': 'npm:/typescript', // assuming typescript > 2.2.1
  },
  packages: {
    ...
    '@angular/common/locales': {
      defaultExtension: 'js'
    },
    "typescript": {
      "main": "lib/typescript.js",
      "meta": {
        "lib/typescript.js": {
          "exports": "ts"
        }
      }
    }
  }
}

Plunker SystemJS@0.19.x, typescript@2.6.2 (在当地测试)

SystemJS 0.20.x

有很多重大更改,因此您必须配置特殊插件https://github.com/frankwallis/plugin-typescript

1)安装插件

npm i -D plugin-typescript

2)更改配置如:

System.config({
  paths: {
    'npm:': 'node_modules/'
  },

  transpiler: 'ts-plugin',

  map: {
    ...
    '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
    '@angular/common/locales': 'npm:@angular/common/locales',
    ...
    'typescript': 'npm:/typescript',
    'ts-plugin': 'npm:plugin-typescript'
  },
  packages: {
    ...
    '@angular/common/locales': {
      defaultExtension: 'js'
    },

    "ts-plugin": {
      "main": "lib/plugin.js"
    },
    "typescript": {
      "main": "lib/typescript.js",
      "meta": {
        "lib/typescript.js": {
          "exports": "ts"
        }
      }
    }
  }
}

正如您现在所看到的,我们使用plugin-typescipt而不仅仅是typescipt。但是内部这个插件需要typescipt所以我们也应该把它保存在我们的配置

Plunker SystemJS@0.20.x typescript@2.6.2 (在当地测试)