带有 require 的 Angular 调用配置不起作用

时间:2021-07-26 03:28:36

标签: angular import config require

我见过使用 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 addnode to the types field in your tsconfigCannot find module

也尝试过像这样使用导入但没有用

import * as data from 'config.json';

我正在寻找最简单的同步原版解决方案 请帮忙谢谢

0 个答案:

没有答案
相关问题