我想将PubNub添加到angular2-cli项目。问题在于链接;但是,我遵循npmjs.com上的pubnub-angular2软件包的说明。将pubnub添加到angular2 cli项目
当我尝试加载它在浏览器中,该错误信息是这样的:
例外:PubNub是不是在全球范围内。确保pubnub.js V4 库角适配器
在app.module.ts之前包括在内,我有以下几点:
import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {HttpModule} from '@angular/http';
import {PubNubAngular} from 'pubnub-angular2';
import {AppComponent} from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule
],
providers: [PubNubAngular],
bootstrap: [AppComponent]
})
export class AppModule {}
在app.component.ts,我有以下代码:
import {Component, Injectable, Class} from '@angular/core';
import {PubNubAngular} from "pubnub-angular2";
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Pubnub Chat Service';
private channel = 'chat-demo-app';
constructor(private pubnubService: PubNubAngular) {
pubnubService.init({
publishKey: 'pub-c-***',
subscribeKey: 'sub-c-***'
});
}
sendMessage(message: string): void {
this.pubnubService.publish({
channel: this.channel,
message: message
});
}
}
需要注意的是,如果我删除了进口的AppComponent,该组件没有看到PubNubAngular提供商。
在此先感谢。
我从来没有使用过这个库,但似乎错误是非常简单的,你didin't导入主库只是角度适配器。您需要将库包含在您的vendor.ts中,例如'import“myLibrary”'或将其添加到index.html。在使用它的组件/服务中导入'pubnub-angular2'之前。 – n00dl3
是的,它真的很直接;然而,解决方案对我来说并不是那么微不足道。 vendor.ts是我无法更改的第三方库,而当我引用index.html中的node_modules目录时,浏览器找不到任何内容(节点模块与src位于同一级别,index.html位于src文件夹)。 –