我有很多运行良好的单元测试。安装
@angular/localize
并将 i18n
属性添加到 html 标签后,所有测试都开始失败,并出现以下错误:
It looks like your application or one of its dependencies is using i18n.
Angular 9 introduced a global `$localize()` function that needs to be loaded.
Please run `ng add @angular/localize` from the Angular CLI.
(For non-CLI projects, add `import '@angular/localize/init';` to your `polyfills.ts` file.
For server-side rendering applications add the import to your `main.server.ts` file.)
即使按照说明进行操作,测试仍然失败。这是我尝试过的:
npm install @angular/localize
和npx nx g @angular/localize:ng-add --project=<project name>
(本项目使用NX)import '@angular/localize/init';
添加到 polyfills.ts
import '../../../node_modules/@angular/localize/init';
添加到 polyfills.ts
main.ts
/// <reference types="@angular/localize" />
添加到 main.ts
import { TextEncoder } from 'util';
global.TextEncoder = TextEncoder;
至
polyfills.ts
、main.ts
和 .spec
文件。
这些都没有解决问题。
有关我安装的内容的一些信息:
Angular: 16.2.8
... animations, common, compiler, compiler-cli, core, forms
... language-service, localize, platform-browser
... platform-browser-dynamic, router
Package Version
---------------------------------------------------------
@angular-devkit/architect 0.1602.5
@angular-devkit/build-angular 16.2.5
@angular-devkit/core 16.2.5
@angular-devkit/schematics 16.2.5
@angular/cdk 16.2.7
@angular/cli 16.2.5
@angular/fire 7.6.1
@angular/material 16.2.7
@schematics/angular 16.2.5
rxjs 7.8.1
typescript 5.1.6
zone.js 0.13.3
如有任何建议,我们将不胜感激。
我最终解决了自己的问题。解决方案是将以下行添加到每个
.spec
文件中:
import '@angular/localize/init';
...
import { TextEncoder } from 'util';
global.TextEncoder = TextEncoder;
如果有人能找到一种全局配置的方法,那就太好了。