11

Angular 11 アプリケーションをライブラリにリファクタリングしています。私の通常のアプリケーションは@angular/localize、dev の依存関係を使用してローカライズされているため$localize、コードの一部でディレクティブを使用しています。このディレクティブを使用してこのコードをライブラリに移動する必要がありますが、その方法がわかりません。

ライブラリのファイルに、アプリケーションの devDependency と一致package.jsonするピア依存関係を追加しました。@angular/localize

{
  "name": "example",
  "version": "0.0.1",
  "peerDependencies": {
    "@angular/common": "^11.0.0",
    "@angular/core": "^11.0.0",
    "@angular/localize": "~11.0.0"
  },
  "dependencies": {
    "tslib": "^2.0.0"
  }
}

$localizeただし、コンパイラは、実行しようとすると見つからないと報告しますng build <library name>

× Compiling TypeScript sources through NGC
ERROR: projects/example/src/lib/example.service.ts:52:23 - error TS2304: Cannot find name '$localize'.

52         description = $localize `example localized text`;
                         ~~~~~~~~~

$localizeライブラリでディレクティブを使用するにはどうすればよいですか?

>ng version

     _                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/
    

Angular CLI: 11.0.1
Node: 12.18.3
OS: win32 x64

Angular: 11.0.0
... animations, cdk, common, compiler, compiler-cli, core, forms
... localize, material, platform-browser
... platform-browser-dynamic, router
Ivy Workspace: Yes

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1100.1
@angular-devkit/build-angular   0.1100.1
@angular-devkit/core            11.0.1
@angular-devkit/schematics      11.0.1
@angular/cli                    11.0.1
@schematics/angular             11.0.1
@schematics/update              0.1100.1
ng-packagr                      11.0.3
rxjs                            6.6.3
typescript                      4.0.5
4

5 に答える 5

1

それを解決しました:

  1. typings.d.tsライブラリのsrcフォルダーの隣にファイルを作成します。
  2. 入れる:declare function $localize(messageParts: TemplateStringsArray, ...expressions: readonly any[]): string;

エラーが消え、コンパイラは問題なく翻訳部分を見つけます。

于 2021-06-03T15:32:20.620 に答える