【问题标题】:error TS2351: This expression is not constructable错误 TS2351:此表达式不可构造
【发布时间】:2020-12-06 03:47:29
【问题描述】:

我正在尝试开发一个聊天机器人。请看下面,看看我哪里错了

import { Injectable } from '@angular/core';
import * as Pusher from 'pusher-js';

// this is here to discourage the instantianting of pusher any where its
// needed, better to reference it from one place
@Injectable()
export class PusherService {
private _pusher: any;

constructor() {
  this._pusher = new Pusher ('APP_KEY', {
    cluster: 'eu',
    encrypted: true
  });
}

getPusher() {
  return this._pusher;
}

}

【问题讨论】:

  • 这个问题缺少提供答案所需的信息——为什么你说事情“出了问题”?运行代码时什么都没有发生?你有错误吗?

标签: angular typescript


【解决方案1】:

使用import * 导入整个命名空间。要访问其默认导出,您必须使用Pusher.default。但除非你需要整个命名空间,否则只导入类怎么样?

import Pusher from 'pusher-js';

作为the docs recommend

【讨论】:

    猜你喜欢
    • 2021-05-14
    • 2020-11-21
    • 1970-01-01
    • 2021-09-04
    • 2020-09-16
    • 2022-09-30
    • 1970-01-01
    • 2017-03-25
    • 2017-03-10
    相关资源
    最近更新 更多