我见过使用 clientHttp 加载这样的配置文件的例子
this.clientHttp.get<any>('./assets/data/config.json').subscribe(data => {
if(data.serverUrl && data.serverUrl != '') {
this.serverUrl = data.serverUrl;
}
});
在我看来,这对于不需要的异步问题来说是一个过度杀戮和一个坑 tar,尤其是在配置文件是客户端的情况下。
试图用一个简单的替换它
var CONFIG = require('./config.json');
var dbPort = CONFIG.dbPort;
像@TimoStaudinger answered here 但我得到 Cannot find name 'require'. Do you need to install type definitions for node? Try
npm i @types/nodeand then add
node to the types field in your tsconfig
或 Cannot find module
也尝试过像这样使用导入但没有用
import * as data from 'config.json';
我正在寻找最简单的同步原版解决方案 请帮忙谢谢