404尝试在Angular 5中加载区域设置时出错

404尝试在Angular 5中加载区域设置时出错,angular,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'; register

我正在尝试在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>

使用angular软件包中的区域设置需要对SystemJS进行额外配置,因为它们不包括在主捆绑包中,也没有专用捆绑包

它们还作为ES模块装运,必须进行传输

SystemJS 0.19.x 简单版本:

System.config({
路径:{
'npm:':'node_modules/'
},
transpiler:'typescript',//我们需要它来传输区域设置
地图:{
...
“@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'
^^^^^^^
必修的
},
套餐:{
...
“@angular/common/locales”:{
defaultExtension:'js'
},
}
}
(我也在本地测试过)

如果你不想使用typescript@2.2.1(而不是>2.2.1),然后从unpkgcom得到它,然后考虑下面的配置。

稍微复杂的版本0.19.x:

System.config({
路径:{
'npm:':'node_modules/'
},
transpiler:'typescript',
地图:{
...
“@angular/common”:“npm:@angular/common/bundles/common.umd.js”,
“@angular/common/locales”:“npm:@angular/common/locales”,
...
“typescript”:“npm:/typescript”,//假设typescript>2.2.1
},
套餐:{
...
“@angular/common/locales”:{
defaultExtension:'js'
},
“打字脚本”:{
“main”:“lib/typescript.js”,
“元”:{
“lib/typescript.js”:{
“出口”:“ts”
}
}
}
}
}
(本地测试)

SystemJS 0.20.x 有很多突破性的变化,所以你必须配置特殊的插件

1) 安装插件

npmi-D插件类型脚本
2) 更改配置,如:

System.config({
路径:{
'npm:':'node_modules/'
},
transpiler:“ts插件”,
地图:{
...
“@angular/common”:“npm:@angular/common/bundles/common.umd.js”,
“@angular/common/locales”:“npm:@angular/common/locales”,
...
“typescript”:“npm:/typescript”,
“ts插件”:“npm:插件类型脚本”
},
套餐:{
...
“@angular/common/locales”:{
defaultExtension:'js'
},
“ts插件”:{
“main”:“lib/plugin.js”
},
“打字脚本”:{
“main”:“lib/typescript.js”,
“元”:{
“lib/typescript.js”:{
“出口”:“ts”
}
}
}
}
}
正如您现在看到的,我们使用的是
plugin-typescipt
,而不仅仅是typescipt。但是在内部,这个插件需要
typescipt
,所以我们也应该将它保存在配置中


(本地测试)

我不熟悉VS2015如何设置angular项目。你知道你是在使用SystemJS还是Webpack吗?还是在使用angular cli?我怀疑它使用SystemJS。然后他需要将
map
添加到
@angular/common/locales
中,并传输区域设置文件,因为它是作为ES模块分发的。我已经有了systemjsI在那里添加了这一行:
“@angular/locale/el”:“npm:@angular/common/locales/el.js”
一个问题,VS有自己的类型脚本,我看到它是版本2.2.2。我如何让systemjs使用相同的一个,以便在将来的更新中不必记住在两个地方都做了更改?如果您在系统上,也许第二个配置对您有用js 0.19检查了我的package.json。我在“systemjs”:“0.20.19”上。我得到了这个错误:
错误:预期表达式,得到了“复制了我的上一个配置吗?你能把你的精简项目发布到某个地方让我检查一下吗?
/**
 * 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);