Angular $localize不是一个函数。

3

在我的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。有任何想法吗?

1个回答

16

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

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

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

导致它无法工作


可以确认这一点。应该是一个被接受的答案。另外,如果你有这个导入(import),在本地运行时不会出现任何错误。 - Mr. L
我在网上尝试了所有方法,最终通过以下命令解决了我的“lint”问题:“npx eslint src/**/*.{js,jsx,ts,tsx,html} --quiet --fix”。 - Lauri Elias

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接