簡體   English   中英

Angular/Typescript:無法編譯“export const”

[英]Angular/Typescript: "export const" cannot be compiled

我剛剛開始為 Angular 構建自己的庫。 首先我添加了一個服務; 我可以毫無問題地構建 lib。

但是,對於此服務,我已將一個常量移至該服務現在引用的名為 tokens.ts 的文件中。 Ĵ

現在構建失敗並顯示以下消息:

------------------------------------------------------------------------------
Building entry point 'mylib'
------------------------------------------------------------------------------
√ Compiling with Angular sources in Ivy partial compilation mode.
× Generating FESM2020
'SOME_VALUE' is not exported by dist\mylib\esm2020\lib\tokens.mjs, imported by dist\mylib\esm2020\lib\services\mysuperservice.service.mjs
Process finished with exit code 1

令牌.ts:

export declare const SOME_VALUE: ...

提到的服務:

import {SOME_VALUE} from "../tokens";

@Injectable({
  providedIn: 'root',
})
export class MySuperService{
//...

我的 tsconfig.lib.json:

{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "outDir": "../../out-tsc/lib",
    "declaration": true,
    "declarationMap": true,
    "inlineSources": true,
    "types": []
  },
  "exclude": [
    "src/test.ts",
    "**/*.spec.ts"
  ]
}

我的 tsconfig.json:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "paths": {
      "common": [
        "dist/mylib/mylib",
        "dist/mylib"
      ]
    },
    "outDir": "./dist/out-tsc",
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "noImplicitOverride": true,
    "noPropertyAccessFromIndexSignature": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2017",
    "module": "es2020",
    "lib": [
      "es2020",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "enableI18nLegacyMessageIdFormat": false,
    "strictInjectionParameters": true,
    "strictInputAccessModifiers": true,
    "strictTemplates": true
  }
}

我的公共 api.ts:

export * from './lib/tokens';
export * from './lib/mylib/mysuperservice.service';
export * from './lib/mylib.module';

我的項目樹:

項目樹

我可以想象這與我的 TS 編譯器選項有關,因為錯誤消息提到了 .mjs 文件。 但是,我不知道問題可能出在哪里。

你能幫我解決這個問題嗎?

正如@jboot 提到的,問題確實是我在聲明一個類型; 不是具體實例。

正確方法:

export const SOME_VALUE= new InjectionToken<SomeType>('some description');

感謝那!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM