我得到了错误
“属性'创建'不存在于类型'typeof ChatEngineCore'”和
类型'typeof ChatEngineCore'上不存在“属性'插件'”
当我尝试在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);
});
}
}
我知道这是一个老问题,但是,由于我处理了同样的问题,这个解决方法可能会帮助其他人。
由于ChatEngineCore
是全局声明的,我所做的就是删除它的导入,然后直接从window
对象调用它:
this.instance = window['ChatEngineCore'].create({ ... });
希望将来更新chat-engine
包将解决此导入错误。
ChatEngineCore具有属性create和plugin但它们不直接存在于其中。
它们位于ChatEngineCore类的对象实例的原型中。
所以要访问我们需要使用命令:
ChatEngineCore.prototype.create()
ChatEngineCore.prototype.plugins
:)