微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Angular 库,找不到名称 $localize

如何解决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';

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。