我当前正在尝试将strophe与ionic 2一起使用,我同时具有strophejquery以及@types/strophe和类型的npm。

这是我的代码

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import {Strophe} from 'strophe';


@Component({
  selector: 'page-home',
  templateUrl: 'home.html',
})
export class HomePage {


  public connection: any ;

  public BOSH_SERVICE: any="http://52.32.***.5:5280/http-bind/" ;


constructor(public navCtrl: NavController) {

}


login() {


  this.connection = new Strophe.Connection(this.BOSH_SERVICE);
  this.connection.connect('ama****'+'@'+'localhost', 'o***oop', this.onConnect);

  console.log("Hello World !");

 }



onConnect(status) {
  console.log('onConnect: '+status);
}


当我尝试离子服务时,出现此错误

TypeError: __WEBPACK_IMPORTED_MODULE_2_strophe__.Strophe is undefined
Stack trace:
HomePage.prototype.login@http://localhost:8100/build/main.js:55747:9
View_HomePage_0/<@ng:///AppModule/HomePage.ngfactory.js:135:21
viewDef/handleEvent@http://localhost:8100/build/main.js:12170:98
callWithDebugContext@http://localhost:8100/build/main.js:13462:39
debugHandleEvent@http://localhost:8100/build/main.js:13050:12
dispatchEvent@http://localhost:8100/build/main.js:9070:12
renderEventHandlerClosure/<@http://localhost:8100/build/main.js:9662:20
decoratePreventDefault/<@http://localhost:8100/build/main.js:33505:53
f</t.prototype.invokeTask@http://localhost:8100/build/polyfills.js:3:9644
                                                                                                                                                                 NgZone.prototype.forkInnerZoneWithAngularBehavior/this.inner<.onInvokeTask@htt           p://localhost:8100/build/main.js:4397:28
f</t.prototype.invokeTask@http://localhost:8100/build/polyfills.js:3:9557
c</r.prototype.runTask@http://localhost:8100/build/polyfills.js:3:4812
t/this.invoke@http://localhost:8100/build/polyfills.js:3:10626

最佳答案

1)从Strophe Site下载Strophe库

然后,您应该将框架放在/ src / assets /

然后,您应该在polyfills.js和main.js之前将script标签添加到index.html中

从而

<script src="assets/strophejs-1.2.14/strophe.js"></script>


持续,

您应该在使用该引号之前放置此clarify语句。

declare var Strophe: any;


这个对我有用!

2)另一种方法是您可以使用安装

npm install strophe.js


并将strophe.js文件从node_modules / strophe.js文件夹复制到src / assets文件夹中,然后在索引文件中提供路径

09-27 19:11