简体   繁体   English

本机库的 webpack 错误 angular 包装器

[英]webpack error angular wrapper for native lib

I have created a Service for an Angular App which is working fine as expected.我为 Angular 应用程序创建了一个服务,该应用程序按预期工作。 Its a Notification Service Wrapper for the package 'awesome-notifications'.它是 package 'awesome-notifications' 的通知服务包装器。 If I transfer the Service to the lib and use it from the lib i am getting kind of an import error its telling me something like constructor is not defined.如果我将服务转移到库并从库中使用它,我会遇到一种导入错误,它告诉我没有定义构造函数之类的东西。 Its referencing to the line: 'import AWN from "awesome-notifications"' which is the first line of the service.它引用了以下行:'import AWN from "awesome-notifications"',这是服务的第一行。 its needed to create the notifier in the Service.它需要在服务中创建通知程序。 Because its a Wrapper.因为它是一个包装器。 it seems like angular can not inject the service i wrote.似乎 angular 无法注入我编写的服务。 But if i am using it in an existing app it works, its really strange.但是如果我在现有的应用程序中使用它,它真的很奇怪。 does someone have experience with that?有人有这方面的经验吗?

Code代码

import { Injectable } from '@angular/core';
import AWN from 'awesome-notifications';

export interface NotificationRequestModel {
  message:string;
  title?:string;
}

@Injectable({
 providedIn: 'root'
})
export class NotificationService {

 notifier = new AWN();

 constructor() {
 }

 success(req:NotificationRequestModel) {
   this.notifier.success(req.message, {
   labels: { success: req?.title },
  })
 } etc..

ERROR TypeError: awesome_notifications__WEBPACK_IMPORTED_MODULE_0__ is not a constructor错误类型错误:awesome_notifications__WEBPACK_IMPORTED_MODULE_0__ 不是构造函数

anybody can help?有人可以帮忙吗? this error seems to exists with every native library i am using in any angular library.我在任何 angular 库中使用的每个本机库似乎都存在此错误。

it is working in an angular application (dev + prod), but not from npm package (build with angular cli)它在 angular 应用程序(开发 + 产品)中工作,但不是来自 npm package(使用 ZD2218B86224A05BF5 构建)

This looks like a build/bundle issue with webpack/typescript.这看起来像是 webpack/typescript 的构建/捆绑问题。 Since one can't reproduce your setup you should provide an example with more information.由于无法重现您的设置,您应该提供一个包含更多信息的示例。 Its probably the default import for your native library.它可能是您本机库的默认导入。 I can only guess:我只能猜测:

Do you have allowSyntheticDefaultImports set in your ts config?您的 ts 配置中是否设置了allowSyntheticDefaultImports

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

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