Angular 9 和 Storybook:添加 @angular/localize/init polyfill

Angular 9 and Storybook: Add @angular/localize/init polyfill

更新到 angular 9 时,出现以下故事书错误。

Error message from storybook

错误说我需要为国际化添加一个 polyfill,但我不知道该怎么做。

我需要配置 webpack 并在那里添加 polyfill,但我无法让它工作。

有人知道如何解决这个问题吗?

干杯

编辑: 澄清一下,我在 angular 项目中 运行 ng add @angular/localize

尽管如此,Storybook 并未接受它。

这是我的 polyfills.ts 文件。

/***************************************************************************************************
 * Load `$localize` onto the global scope - used if i18n tags appear in Angular templates.
 */
import '@angular/localize/init';
/**
 * This file includes polyfills needed by Angular and is loaded before the app.
 * You can add your own extra polyfills to this file.
 *
 * This file is divided into 2 sections:
 *   1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers.
 *   2. Application imports. Files imported after ZoneJS that should be loaded before your main
 *      file.
 *
 * The current setup is for so-called "evergreen" browsers; the last versions of browsers that
 * automatically update themselves. This includes Safari >= 10, Chrome >= 55 (including Opera),
 * Edge >= 13 on the desktop, and iOS 10 and Chrome on mobile.
 *
 * Learn more in https://angular.io/docs/ts/latest/guide/browser-support.html
 */

/***************************************************************************************************
 * BROWSER POLYFILLS
 */

/** IE9, IE10 and IE11 requires all of the following polyfills. **/
import 'core-js/es/symbol';
import 'core-js/es/object';
import 'core-js/es/function';
import 'core-js/es/parse-int';
import 'core-js/es/parse-float';
import 'core-js/es/number';
import 'core-js/es/math';
import 'core-js/es/string';
import 'core-js/es/date';
import 'core-js/es/array';
import 'core-js/es/regexp';
import 'core-js/es/map';
import 'core-js/es/set';

/** IE10 and IE11 requires the following for NgClass support on SVG elements */
// import 'classlist.js';  // Run `npm install --save classlist.js`.

/** Evergreen browsers require these. **/
// Used for reflect-metadata in JIT. If you use AOT (and only Angular decorators), you can remove.
import 'core-js/es/reflect';

/**
 * Required to support Web Animations `@angular/platform-browser/animations`.
 * Needed for: All but Chrome, Firefox and Opera. http://caniuse.com/#feat=web-animation
 **/
// import 'web-animations-js';  // Run `npm install --save web-animations-js`.



/***************************************************************************************************
 * Zone JS is required by default for Angular itself.
 */
import 'zone.js/dist/zone';  // Included with Angular CLI.



/***************************************************************************************************
 * APPLICATION IMPORTS
 */
/* https://github.com/aws/aws-amplify/issues/678 fix: */
(window as any).global = window;
/* https://github.com/aws/aws-amplify/issues/678 fix end */

编辑 2:

tsconfig.json

{
  "extends": "../src/tsconfig.app.json",
  "compilerOptions": {
    "types": [
      "node"
    ]
  },
  "exclude": [
    "../src/test.ts",
    "../src/**/*.spec.ts",
    "../projects/**/*.spec.ts",
    "../src/assets/webgl_2019_1/**/*"
  ],
  "include": [
    "../src/**/*",
    "../projects/**/*"
  ],
  "files": [
    "./typings.d.ts"
  ]
}

您必须在 polyfills.ts 中安装 @angular/localize 并导入 '@angular/localize/init' 您还可以:ng add @angular/localize(为您安装并添加到 polyfill) 要么 将 '@angular/localize/init' 导入 config.js

TLDR:import '@angular/localize/init'; 添加到 .storybook 文件夹中的 config.jspreview.js

说明

问题是 Storybook 找不到 polyfill @angular/localize/init。由于 Storybook 使用 webpack 为 Storybook 提供服务,我们需要将 polyfill 添加到 webpack。

将polyfill 添加到webpack 可以通过将其添加到entries 数组来完成。 https://webpack.js.org/guides/shimming/

通过查看此页面 (https://storybook.js.org/docs/configurations/custom-webpack-config/),您可以了解如何配置故事书以添加您的 polyfill。这里有个header叫This is what the config for storybook looks like when using CRA in dev-mode:

如果展开它,它会显示 storybook 中 webpack 的默认配置。在条目数组中,您可以看到有以下行: '<your-storybook-dir>/preview.js'。如果您在此文件中添加 polyfill,它也会添加到 webpack。

preview.jsconfig.js 被视为同一个文件,因此将 polyfill 添加到其中任何一个都可以解决问题。

我通过添加 package.json 这一行来解决这个问题

"dependencies": {
 ...
 "@angular/localize": "^10.2.4",
}

然后我运行:

npm install

因为这个问题我换了操作系统。昨天我找到了这个问题的原因。由于我在 Visual Studio 代码中安装了一个插件,我遇到了这个问题。如果您使用的是 Angular9,请不要安装前两个插件。你可以安装第三个。