Angular 库,找不到名称 $localize

问题描述

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

库 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

解决方法

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

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

,

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

 import '@angular/localize/init';

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...