angular - 带有 ng2-stompjs 配置的 Angular 7
问题描述
我在网页下关注教程:https ://stomp-js.github.io/guide/ng2-stompjs/2018/11/04/ng2-stomp-with-angular7.html 。
一切都很好,直到我得到了必须将应用程序的所有配置(包括 websocket URL)外部化的部分。由于我对 Angular 的了解不足,我发现很难做到这一点。问题是,在教程中它是硬编码的:
export const myRxStompConfig: InjectableRxStompConfig = {
// Which server?
brokerURL: 'ws://127.0.0.1:15674/ws',
我准备了这个类来获取 API url:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class AppConfig {
private config: object = null;
private env: object = null;
constructor(private http: HttpClient) {}
public getConfig(key: any) {
return this.config[key];
}
public getEnv(key: any) {
return this.env[key];
}
getConfigs(): Promise<Object> {
return new Promise((resolve, reject) => {
this.http.get('assets/config/env.json').subscribe((envResponse: any)
=> {
this.env = envResponse;
let request: any = null;
switch (envResponse.env) {
case 'production':
{
request = this.http.get(
'assets/config/api.' + this.getEnv('env') + '.json'
);
}
break;
case 'development':
{
request = this.http.get(
'assets/config/api.' + this.getEnv('env') + '.json'
);
}
break;
case 'default':
{
console.error('Environment file is not set or invalid');
resolve(true);
}
break;
}
if (request) {
request.subscribe(responseData => {
this.config = responseData;
resolve(true);
});
} else {
console.error('Env config file "env.json" is not valid');
resolve(true);
}
});
});
}
}
我正在尝试这样的事情:
export class Stomp {
public static brokerURL = null;
public static heartbeatIncoming: 0; // Typical value 0 - disabled
public static heartbeatOutgoing: 0; // Typical value 20000 - every 20 seconds
public static reconnectDelay: 5000;
constructor(private appConfig: AppConfig) {
Stomp.brokerURL = this.appConfig.getConfig('openbatonWS') + '/websocketRD/websocket';
}
但没有运气。你能指出我正确的方向吗?如何在 InjectableRxStompConfig 中实现外部化 brokerURL。
谢谢, 城市
解决方案
谢谢@bam123!
我通过以下方式实现它:
export class MyRxStompConfig extends InjectableRxStompConfig {
constructor(private config: ConfigService) {
super();
this.brokerURL = this.config.brokerURL;
this.heartbeatIncoming = 0;
this.heartbeatOutgoing = 10000;
this.reconnectDelay = 500;
}
}
我将以下内容放入我的 app.module
{ provide: InjectableRxStompConfig, useClass: MyRxStompConfig, deps: [ConfigService] }
推荐阅读
- ios - 向 WKWebView 发送消息会引发错误
- angular - 从异步第一个 api 调用获取响应(需要时间来获取响应)并将其传递给第二个异步 api 以获得最终响应
- apple-watch - Xcode 11 - 为 Apple Watch 添加新目标
- c# - .NET Core 中的更新尝试引发异常
- javascript - JavaScript getElementByID - JavaScript 代码中的错误(字段留空,但单击时未变为红色)
- java - 如果从 IntelliJ IDEA 运行应用程序,则找不到 Mapstruct 生成的类
- java - collection.add(()->7); 中 ()-> 的功能是什么?
- dialogflow-es - 在 java 中使用服务帐户凭据(.json 文件)创建/更新 Dialogeflow 代理
- rabbitmq - RabbitMQ 支持 LIFO 或基于时间的优先级队列
- php - 如何通过mysql语句从文件夹中删除图像