简体   繁体   English

Angular/Typescript:无法编译“export const”

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

I've just started to build my own library for Angular.我刚刚开始为 Angular 构建自己的库。 First I added a service;首先我添加了一个服务; I can build the lib problem-free.我可以毫无问题地构建 lib。

For this service, however, I have moved a constant to a file called tokens.ts to which the service now references.但是,对于此服务,我已将一个常量移至该服务现在引用的名为 tokens.ts 的文件中。 J Ĵ

Now the build fails with the following message:现在构建失败并显示以下消息:

------------------------------------------------------------------------------
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

The tokens.ts:令牌.ts:

export declare const SOME_VALUE: ...

The mentioned service:提到的服务:

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

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

My tsconfig.lib.json:我的 tsconfig.lib.json:

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

My tsconfig.json:我的 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
  }
}

My public-api.ts:我的公共 api.ts:

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

My project-tree:我的项目树:

项目树

I could imagine that this has something to do with my TS compiler options, since the error message mentions.mjs files.我可以想象这与我的 TS 编译器选项有关,因为错误消息提到了 .mjs 文件。 However, I have no idea where the problem could be.但是,我不知道问题可能出在哪里。

Can you help me with that?你能帮我解决这个问题吗?

As @jboot mentioned, the problem was indeed that I was declaring a type;正如@jboot 提到的,问题确实是我在声明一个类型; not a concret instance.不是具体实例。

Correct way:正确方法:

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

Thanks for that!感谢那!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM