[英]How to integrate StropheJs with Angular 8
我嘗試過的方法:
方法一:
從http://strophe.im/strophejs/下載了strophejs-1.3.4.zip
將解壓后的文件夾,即strophejs-1.3.4
放在我的 angular8 項目的src/assets
文件夾中。
在我的index.html
文件中包含
<!--// Strophe.js-->
<script src='assets/strophejs-1.3.4/src/strophe.js'></script>
我使用以下命令安裝了@types/strophe: npm install --save-dev @types/strophe
然后在我的 component.ts 文件中聲明 let Strophe: any;
按照這些步驟編譯成功,但是在運行時,我收到運行時參考錯誤: Reference Error: Strophe is not defined
下面是我的 component.ts 文件
import { Component, OnInit } from '@angular/core';
declare let Strophe: any;
@Component({
selector: 'app-oauth',
templateUrl: './oauth.component.html',
styleUrls: ['./oauth.component.scss']
})
export class OauthComponent implements OnInit {
constructor() { }
ngOnInit() {
const connection = new Strophe.Connection('http://localhost:5280/bosh');
// connection.rawInput = rawInput;
// connection.rawOutput = rawOutput;
connection.connect('user@localhost', 'password', this.onConnect);
}
onConnect(status) {
if (status == Strophe.Status.CONNECTING) {
console.log('Strophe is connecting.');
} else if (status == Strophe.Status.CONNFAIL) {
console.log('Strophe failed to connect.');
} else if (status == Strophe.Status.DISCONNECTING) {
console.log('Strophe is disconnecting.');
} else if (status == Strophe.Status.DISCONNECTED) {
console.log('Strophe is disconnected.');
} else if (status == Strophe.Status.CONNECTED) {
console.log('Strophe is connected.');
}
}
}
方法二:
使用https://www.npmjs.com/package/strophe 中的命令npm i strophe
安裝了 npm pkg strophejs(我不願意使用它,因為它提到它已被npm i strophe
)
然后在我的 component.ts 文件中,我無法導入它。 :(
PS @types/strophe 的 index.d.ts 文件中顯示錯誤(
Exports and export assignments are not permitted in module augmentations.
):
declare module "Strophe" {
export = Strophe;
^^^^^^
}
為了解決這個問題,我參考了以下內容:
模塊擴充中不允許導出和導出分配- 我不明白如何在我的場景中實現這一點
打字稿錯誤“不能在具有其他導出元素的模塊中使用導出分配。” 在擴展打字稿定義時- 嘗試過但沒有用
嘗試了此https://github.com/apexcharts/apexcharts.js/issues/577 中給出的修復程序,但這也不起作用。
不推薦使用 Strophe npm 包https://www.npmjs.com/package/strophe ,因此不建議使用它。 我想出了如何使用從http://strophe.im/strophejs/下載的文件
步驟如下:
導航到下載的文件夾。
然后在其中運行 npm install 這將創建一個 dist 文件夾,其中創建了兩個文件和一個文件夾。
從/dist/Strophe.umd.js
文件夾復制Strophe.umd.min.js
文件並將其粘貼到 angular 項目的 /assets 文件夾中。
在angular.json
文件中添加此文件的路徑。
"scripts": [
"src/assets/strophe.umd.min.js"
]
在@canbax 在他的回答中提到的路徑下。
要使用 npm 包,您應該添加到angular.json
projects>your-project-name>architect>build>options>scripts
如下所示。 在JSON文件中,它有點深。
"scripts": [ "node_modules/jquery/dist/jquery.js",
對於像 jquery 這樣的普通 JS 庫,您可以將其導入到 TS 文件中,例如
import * as $ from 'jquery';
與類型和類型支持一起使用可能會很復雜。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.