【问题标题】:Trouble with ChatEngineCore in the Pubnub Angular2 TutorialPubnub Angular2 教程中的 ChatEngineCore 问题
【发布时间】:2018-08-11 02:16:10
【问题描述】:

我得到错误

“类型'typeof ChatEngineCore'上不存在属性'create'”和

“类型'typeof ChatEngineCore'上不存在属性'plugin'”

当我尝试在 PubNub Angular2 Chatbot 教程中调用 ChatEngineCore.create 和 ChatEngineCore.plugin 时。

有什么想法吗?我错过了一些简单的东西吗?这是我直接从 PubNub 的教程中复制的代码:

import { Injectable } from '@angular/core';
import { ChatEngineCore } from 'chat-engine';

@Injectable()
export class ChatEngine {
  instance: any;
  create: any;
  plugin: any;
  me: any = { state: {} };
  chat: any = {};
  chats: any[] = [];
  constructor() {
    // Make sure to import ChatEngine first!
    this.instance = ChatEngineCore.create({
      publishKey: 'MY-PUBLISH-KEY',
      subscribeKey: 'MY-SUBSCRIBE-KEY'
    },
    {
      debug: true,
      globalChannel: 'chat-engine-angular2-simple'
    });
    this.create = ChatEngineCore.create.bind(this);
    this.plugin = ChatEngineCore.plugin;
  }

  newChat(user) {
    // define a channel
    let chat = new Date().getTime();
    // create a new chat with that channel
    let newChat = new this.instance.Chat(chat);
    // we need to auth ourselves before we can invite others
    newChat.on('$.connected', () => {
      // this fires a private invite to the user
      newChat.invite(user);
      // add the chat to the list
      this.chats.push(newChat);
    });
  }
}

【问题讨论】:

  • 能否提供教程链接和您使用的ChatEngine版本?

标签: angular pubnub


【解决方案1】:

我知道这是一个老问题,但是,因为我已经处理过同样的问题,所以这个解决方法可以帮助其他人吗?

由于ChatEngineCore 是全局声明的,我所做的是删除它的导入,然后直接从window 对象中调用它:

this.instance = window['ChatEngineCore'].create({ ... });

希望chat-engine 包的未来更新将解决此导入错误。

【讨论】:

    【解决方案2】:

    ChatEngineCore 具有 create 和 plugin 属性,但它们并不直接存在于其中。

    它们在 ChatEngineCore 类的对象实例的原型中。

    所以要访问我们需要使用命令:

    ChatEngineCore.prototype.create()
    ChatEngineCore.prototype.plugins
    

    :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-10
      • 2011-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多