小编Nun*_*ves的帖子

当导入 Angular 13 项目时,Angular 13 包/库抛出错误“moment is not a function”

我正在升级私有角度库/包(my-lib),以便我可以迁移所有其他项目,但是当导入到项目中时,其中一项服务使用 moment 并抛出错误:“错误类型错误:moment 不是函数”

该库在开发模式下工作,构建和发布都正常,即使导入到项目中,所有组件和资源都加载良好并且没有显示错误,依赖项已下载,但似乎某些 3rd 方依赖项不被“我的”所知-lib”导入后。

即使在项目中,我也可以导入 moment 并使用它,但 node_modules 中的“my-lib”看不到该包。

“my-lib”中的一些文件

服务.ts

import * as moment_ from 'moment';

const moment = moment_;
...

moment(value).format('YYYY-MM-DD')  ----> line that throws the error
Run Code Online (Sandbox Code Playgroud)

已经尝试过

import moment from 'moment';
Run Code Online (Sandbox Code Playgroud)

以及 tsconfig 中的“allowSyntheticDefaultImports”标志

包.json

"dependencies": {
    ...
    "moment": "~2.29.1",
    ...
},
"devDependencies": {
    "@angular-builders/custom-webpack": "~13.0.0",
    "@angular-devkit/build-angular": "~13.1.1",
    "@angular-eslint/builder": "~13.0.1",
    "@angular-eslint/eslint-plugin": "~13.0.1",
    "@angular-eslint/eslint-plugin-template": "~13.0.1",
    "@angular-eslint/schematics": "~13.0.1",
    "@angular-eslint/template-parser": "~13.0.1",
    "@angular/animations": "~13.1.0",
    "@angular/cli": "~13.1.1",
    "@angular/common": "~13.1.0",
    "@angular/compiler": "~13.1.0",
    "@angular/compiler-cli": "~13.1.0",
    "@angular/core": "~13.1.0",
    "@angular/forms": "~13.1.0",
    "@angular/language-service": "~13.1.0", …
Run Code Online (Sandbox Code Playgroud)

momentjs typescript angular angular-library ng-packagr

9
推荐指数
1
解决办法
7112
查看次数