【问题标题】:Angular library, Cannot find name $localizeAngular 库,找不到名称 $localize
【发布时间】:2021-05-01 00:21:25
【问题描述】:

我正在使用 Nx 工具将我的 Angular 11 应用程序重构为库。我的主应用程序将@angular/localize 作为依赖项,并将@angular/localize 导入polyfills.ts。在我的库代码中每次使用 $localize 都会生成 Typescript 错误 TS2304: Cannot find name '$localize'。我的图书馆没有自己的package.json 文件。应用程序构建工作没有错误。它看起来像打字稿打字问题。有什么想法吗?

库 tsconfig.lib.json

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "../../../dist/out-tsc",
    "target": "es2015",
    "declaration": true,
    "declarationMap": true,
    "inlineSources": true,
    "types": [],
    "lib": [
      "dom",
      "es2018"
    ]
  },
  "angularCompilerOptions": {
    "skipTemplateCodegen": true,
    "strictMetadataEmit": true,
    "enableResourceInlining": true
  },
  "exclude": [
    "src/test.ts",
    "**/*.spec.ts"
  ],
  "include": [
    "**/*.ts"
  ]
}

库中的tsconfig.json

{
  "extends": "../../../tsconfig.base.json",
  "files": [],
  "include": [],
  "references": [
    {
      "path": "./tsconfig.lib.json"
    },
    {
      "path": "./tsconfig.spec.json"
    }
  ]
}

角度版本

Angular CLI: 11.1.1
Node: 14.15.4
OS: win32 x64

Angular: 11.1.0
... animations, cdk, common, compiler, compiler-cli, core
... elements, forms, language-service, localize, material
... platform-browser, platform-browser-dynamic, router
Ivy Workspace: <error>

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1002.1
@angular-devkit/build-angular   0.1101.1
@angular-devkit/core            10.2.1
@angular-devkit/schematics      11.0.7
@angular/cli                    11.1.1
@schematics/angular             11.0.7
@schematics/update              0.1101.1
rxjs                            6.6.3
typescript                      4.1.3

【问题讨论】:

  • 您是如何解决这个问题的?以下解决方案对我不起作用。
  • 我必须在我的 test-setup.ts 文件中添加 import '@angular/localize/init'; 才能运行测试。在我的index.ts 中导入import './polyfills'; 后运行构建。即使我的问题与您的问题不完全相同,也许这可以帮助您找到问题的解决方案。

标签: angular localization internationalization nrwl nomachine-nx


【解决方案1】:

@angular/localize/init 的导入可能会导致 no-import-side-effect 的 tslint 错误,因为它会添加到全局上下文中(即副作用)。要修复此错误,请将以下内容添加到您的 tslint.config

“无导入副作用”:[ 真的, { "忽略模块": "(core-js/.|zone\.js/.|@angular/localize/init)$" } ]

【讨论】:

    【解决方案2】:

    我来这里是为了寻找答案,但不知何故在路上偶然发现了一个解决方案。在我的项目中,workspace/projects TS 版本与 VS Code 的版本(4.4.4 vs 4.5.4)略有不同,这有时会导致这些非问题。

    除了背景之外,在我的场景中,我所要做的就是打开命令面板,输入/选择“TypeScript:选择 TypeScript 版本...”并将其设置回参考工作区版本。

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,我在polyfills.ts 的第一行添加了以下代码解决了我的问题。

       import '@angular/localize/init';
      

      【讨论】:

        猜你喜欢
        • 2016-01-24
        • 2022-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-14
        • 2018-03-30
        • 1970-01-01
        相关资源
        最近更新 更多