从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
相关产品推荐
相关产品推荐

