【问题标题】:Angular 13 Cannot find module '@angular/common/locales/fr.js'Angular 13 找不到模块'@angular/common/locales/fr.js'
【发布时间】:2021-11-12 16:35:08
【问题描述】:

我从 angular 12 移动到 angular 13 并且出现了一个新错误。 所以基本上我过去是通过导入语言环境文件来加载用户的文化。

 const localeUri = `@angular/common/locales/${localeId}.js`;
return import(localeUri).then((module) => {
    logger.debug("Culture Angular : '" + localeId + "'");
    registerLocaleData(module.default);
}).catch((err) => {
    logger.fatal("culture not defined for angular", err);
});

此解决方案可用于 angular12,但不再适用于 angular13。 Webpack 找不到任何语言环境。 我尝试了一些在 github 上找到的解决方案,但没有任何积极的结果
https://github.com/highlightjs/highlight.js/issues/3223#issuecomment-953337602

https://github.com/angular/angular-cli/issues/22154

有什么办法吗?

【问题讨论】:

    标签: angular webpack angular13


    【解决方案1】:

    通过更改我们导入的路径并将.js更改为.mjs来使其工作

    之前:

    import(
      /* webpackInclude: /(de|de-AT|en|en-GB)\.js$/ */
      `@angular/common/locales/${locale}.js`
    ).then((module) => {
      registerLocaleData(module.default);
    });
    

    之后:

    import(
      /* webpackInclude: /(de|de-AT|en|en-GB)\.mjs$/ */
      `../../../../../../node_modules/@angular/common/locales/${locale}.mjs`
    ).then((module) => {
      registerLocaleData(module.default);
    });
    

    使用 Angular 13.1.1 和 NX 13.4.2

    这里也已经提到了解决方案:https://github.com/angular/angular-cli/issues/22154

    【讨论】:

      【解决方案2】:

      您现在必须从@angular/common/locales/global/ 导入,并且不再需要registerLocaleData

      Angular 13 之前:

      import { registerLocaleData } from '@angular/common';
      import localeFr from '@angular/common/locales/fr';
      
      registerLocaleData(localeFr, 'fr-FR');
      

      使用 Angular 13:

      import '@angular/common/locales/global/fr';
      
      // and you can use fr-FR whenever you want
      

      【讨论】:

      • 不,这个解决方案对我不起作用,因为我的导入是动态的,例如我写的 fr,但语言环境可以是任何东西。
      • 我也有同样的问题,而且这似乎也不起作用...
      • @heikofritz 你找到解决方案了吗?
      • @Yvan 仍在搜索...
      • @heikofritz 相同 :(
      猜你喜欢
      • 2017-12-25
      • 2018-11-10
      • 2016-10-17
      • 2022-09-27
      • 1970-01-01
      • 1970-01-01
      • 2018-01-23
      • 1970-01-01
      • 2018-10-28
      相关资源
      最近更新 更多