如何在Angular2中集成PubNub?

时间:2017-05-05 13:21:21

标签: angular typescript pubnub

我尝试使用PubNub导入/集成我的angular2 MEAN Web应用程序。我得到了一个错误:

  

EXCEPTION:未捕获(承诺):错误:PubNub不在全局范围内   范围。确保在angular之前包含pubnub.js v4库   适配器

我提到了这些链接https://www.pubnub.com/docs/angular2-javascript/pubnub-javascript-sdk

我包括angular2 pubnub' .js' index.html文件中的文件全局如下:

<script src="node_modules/core-js/client/shim.min.js"></script>
>     <script src="node_modules/zone.js/dist/zone.js"></script>
>     <script src="node_modules/reflect-metadata/Reflect.js"></script>
>     <script src="node_modules/rxjs/bundles/Rx.js"></script>
> 
>     <script src="node_modules/@angular/core/bundles/core.umd.js"></script>
>     <script src="node_modules/@angular/common/bundles/common.umd.js"></script>
>     <script src="node_modules/@angular/compiler/bundles/compiler.umd.js"></script>
>     <script src="node_modules/@angular/platform-browser/bundles/platform-browser.umd.js"></script>
>     <script src="node_modules/@angular/forms/bundles/forms.umd.js"></script>
>     <script src="node_modules/@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js"></script>
> 
>     <script src="node_modules/pubnub/dist/web/pubnub.js"></script>
> 
>     <script src="node_modules/pubnub-angular2/dist/pubnub-angular2.js"></script>

在Module.ts代码中输入如下:

导入的文件是

import { PubNubAngular } from 'pubnub-angular2';
providers: [PubNubAngular]

在我的Component Pubnub Init函数中放置如下:

 pubnubService: PubNubAngular;
constructor(private _cookieService: CookieService,
 private http: Http,
 private _ApiMessageService: ApiMessageService,
 private _errorService: ErrorService,
 pubnubService: PubNubAngular) {
     this.pubnubService = pubnubService;
     this.pubnubService.init({
            publishKey: 'XXX',
      subscribeKey: 'XXX'
     });
     this.pubnubService.subscribe({
      channels: ['Order' + this.orderId],
      withPresence: true,
      triggerEvents: ['message', 'presence', 'status']
    });
}

1 个答案:

答案 0 :(得分:0)

我有同样的问题,我删除了所有的包含,并在angular-cli.json中添加了'pubnub.js'和'pubnub-angular2.js'脚本

enter image description here 如提到的那样 Satckoverflow link