You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从JSON文件读取环境变量后URL路径异常问题求助

运行时环境变量导致URL重定向异常

我需要在运行时设置环境变量,按教程操作后,使用变量时出现URL异常——页面会重定向至localhost:8080/index.html#/foo,而非预期的localhost:8080/#/foo。

触发异常的场景

1. 将变量作为类字段使用

export class CometdService implements OnDestroy {

  protected cometd: CometDExtended = new cometlib.CometD() as CometDExtended;

  private subscriptions: cometlib.SubscriptionHandle[] = [];

  apiUrl: string;

  constructor(private environment: AppConfigService, private store: Store<fromRoot.State>) {
    this.apiUrl = environment.config.apiUrl;
    this.cometd.configure('http://localhost:8080/cometd');

    this.startConnection();
  }
(...)
}

2. 将变量传入路径拼接

export class CometdService implements OnDestroy {

  protected cometd: CometDExtended = new cometlib.CometD() as CometDExtended;

  private subscriptions: cometlib.SubscriptionHandle[] = [];

  constructor(private environment: AppConfigService, private store: Store<fromRoot.State>) {
    this.cometd.configure(`http://${environment.config.apiUrl}/cometd`);

    this.startConnection();
  }
(...)
}

无异常的场景(仅实例化变量但不使用)

export class CometdService implements OnDestroy {

  protected cometd: CometDExtended = new cometlib.CometD() as CometDExtended;

  private subscriptions: cometlib.SubscriptionHandle[] = [];

  constructor(private environment: AppConfigService, private store: Store<fromRoot.State>) {
    let apiUrl = environment.config.apiUrl;
    this.cometd.configure('http://localhost:8080/cometd');

    this.startConnection();
  }
(...)
}

内容的提问来源于stack exchange,提问作者podehi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 22:10:34