Angular 5重大更改-手动导入区域设置 [英] Angular 5 Breaking change - manually import locale

查看:82
本文介绍了Angular 5重大更改-手动导入区域设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆