我正在尝试使用具有不同扩展名的相同 env 文件来处理具有多个不同“客户端”的微前端,例如:
this.client = 'CLIENTONE';
// this is the default body to send to httpHeaders
const httpBody = {
apibase: env.${this.client}.apibase,
};
// turn above body into a header to use in requests
this.headerBody = { headers: httpBody };
// END //
上面的示例不起作用,但显示了我尝试过的解决方案。
这是 env 文件示例:
export const CLIENTONE= {
production: false,
apibase: 'https://apiBase/clientOne',
};
export const CLIENTTWO= {
production: false,
apiBase: 'https://apiBase/clientTwo'
};
回答1
我会这样处理它 - 用选项的“字典”来构建环境文件:
export const environment = {
CLIENTONE: {
production: false,
apibase: 'https://apiBase/clientOne'
},
CLIENTTWO: {
production: false,
apiBase: 'https://apiBase/clientTwo'
}
};
如果您随后使用 'CLIENTONE' | 'CLIENTTWO'
类型定义了您的 client
变量,例如像这样:
client: 'CLIENTONE' | 'CLIENTTWO' = 'CLIENTONE';
然后,您可以在您的示例的基础上访问它,例如:
this.client = 'CLIENTONE';
// this is the default body to send to httpHeaders
const httpBody = {
apibase: environment[this.client].apiBase,
};
// turn above body into a header to use in requests
this.headerBody = { headers: httpBody };
// END //