Angular 5重大更改-手动导入区域设置 [英] Angular 5 Breaking change - manually import locale
问题描述
Changelog说:
Changelog says:
默认情况下,Angular现在仅包含该语言的语言环境数据 在美国,如果您将LOCALE_ID的值设置为其他语言环境,则 必须为此语言导入新的语言环境数据,因为我们不使用 不再是国际API.
By default Angular now only contains locale data for the language en-US, if you set the value of LOCALE_ID to another locale, you will have to import new locale data for this language because we don’t use the intl API anymore.
但是我找不到任何有关导入"的含义,操作方法以及得到的参考
But I can not find any reference what "importing" means, how to do it and I get
xxx.html:30错误错误:缺少该语言环境的数据 语言环境"de-CH"
xxx.html:30 ERROR Error: Missing locale data for the locale "de-CH"
我使用以下语言配置语言环境:
I configure locale with :
import { LOCALE_ID } from '@angular/core';
和
providers: [
{ provide: LOCALE_ID, useValue: 'de-CH' }
],
推荐答案
在当前版本中真的很难找到:(.这是我发现的内容:
This is really hard to find in the current version :(. Here is what i found out:
不同的语言环境位于软件包@angular/common/locales/
中.您的情况是:
The different locales are in the package @angular/common/locales/
. In your case this is:
import localeDECH from '@angular/common/locales/de-CH';
现在,您需要在项目中注册此语言环境定义. @angular/common
中有一个名为registerLocaleData
的函数.
Now you need to register this locale definitions in your project. There is a function called registerLocaleData
that is located in: @angular/common
.
因此,您在app.module.ts
中的代码应如下所示:
So your code in your app.module.ts
should look like this:
import {LOCALE_ID} from '@angular/core';
import { registerLocaleData } from '@angular/common';
import localeDECH from '@angular/common/locales/de-CH';
registerLocaleData(localeDECH);
@NgModule({
...
providers: [
{ provide: LOCALE_ID, useValue: 'de-ch' },
]
...
})
....
这篇关于Angular 5重大更改-手动导入区域设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!