【问题标题】:Angular $localize is not a functionAngular $localize 不是函数
【发布时间】:2021-06-15 08:11:24
【问题描述】:

尝试在我的 ts 文件中使用 $localize 时遇到了一个奇怪的错误。我收到此错误

Error: Uncaught (in promise): TypeError: _angular_localize_init__WEBPACK_IMPORTED_MODULE_3__.$localize is not a function
TypeError: _angular_localize_init__WEBPACK_IMPORTED_MODULE_3__.$localize is not a function

我的模板文件中有本地化工作,调用ng build 没有错误。当调用包含$localize 方法的组件时,错误出现在运行时。我的 ts 代码是

  private handleServerErrors(error: HttpErrorResponse): void {
    // Handle errors
    this.complete = true;
    switch (error.status) {

      ...

      case 500:
      default:
        // Could not update user
        this.errorMessage = $localize`:@@CalmBreathing_Desc_Short:Cobblers`;
        break;
    }
    console.error(error);
  }

polyfills.ts 文件的顶部有这个

/***************************************************************************************************
 * Load `$localize` onto the global scope - used if i18n tags appear in Angular templates.
 */
import '@angular/localize/init';

这是带有 nodejs 12 的 Angular 11.2.8。有什么想法吗?

【问题讨论】:

    标签: angular internationalization angular-i18n


    【解决方案1】:

    所以这是 WebStorm 试图提供帮助的原因。它在我的 ts 文件的顶部添加了以下行

    import { $localize } from '@angular/localize/init';
    

    导致它无法工作

    【讨论】:

    • 可以确认这一点。应该是公认的答案。此外,如果您有此导入,则在本地运行时不会给您带来任何错误。
    猜你喜欢
    • 1970-01-01
    • 2021-05-01
    • 2021-11-20
    • 2020-01-17
    • 2020-06-10
    • 2020-01-10
    • 2015-09-01
    • 2018-03-19
    • 2018-07-05
    相关资源
    最近更新 更多