javascript - ionic 2 与节段不工作?

标签 javascript jquery node.js angular ionic2

我目前正在尝试将 strope 与 ionic 2 一起使用,我有 npm stropejquery 以及 @types/strope 和打字.

这是我的代码

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);
}

当我尝试 ionic 服务时,我收到此错误

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) 从 Strope Site 下载 Strope 库

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

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

因此

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

最后,

您应该将此声明语句放在使用节号之前。

declare var Strophe: any;

这对我有用!

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

安装
npm install strophe.js

并将 strope.js 文件从 node_modules/strope.js 文件夹复制到上面的 src/assets 文件夹,然后在索引文件中给出路径

关于javascript - ionic 2 与节段不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44251412/

相关文章:

sql - 带回调的数据库事务

node.js - 当将代码拆分到 Nodejs 中的自定义模块时,尝试将变量传递给另一个模块时,它会变得未定义

javascript - Node.js 中的阻塞 IO

javascript - JavaScript 中的异步原语是什么?

javascript - 使用 W3.CSS 的多个幻灯片放映问题

JQuery FullCalendar 不在初始 View 上呈现事件

javascript - 更改提交的表单中的值和 JQuery 序列化函数

javascript - Uncaught TypeError:无法读取未定义的属性 'statistics'(jquery,youtube api v3)

javascript - Chromium如何将拖动和/或按住转换为单击?

javascript - 样式组件 : I want to use Variable in Styled-Component