在我的 Angular 混合应用程序的引导过程中(Angular7 和 AngularJS 并排工作)。我想使用来自单独的配置 json 文件的配置作为窗口变量。目前我正在使用:
setAngularLib(AngularJs);
platformBrowserDynamic().bootstrapModule(CatalogModule).then(platformRef => {
const config = require('./config.json');
(<any>window).__APP_CONFIG__ = config;
const appMountPoint = document.querySelector('[xs-app-mountpoint]');
const upgrade = platformRef.injector.get(UpgradeModule) as UpgradeModule;
upgrade.bootstrap(appMountPoint, [Ng1Module], {strictDi: false});
console.log('hybrid app bootstrapped');
})
.catch(err => console.log(err));
但是,目前 Webpack 正在编译后将整个 config.json 文件内容放入一个 main.js 文件中,这是我不想要的。我想获取配置文件并将其用作 Window 变量,但将 config.json 文件与 main.js 分开。
怎么可能?我尝试使用
const config = JSON.parse(fs.readFileSync('./config.json', 'utf8'));
但它没有用。
我设法通过使用 Fetch API 找到了解决方案:
fetch('config.json')
.then(response => response.json())
.then((config) => {
platformBrowserDynamic().bootstrapModule(CatalogModule).then(platformRef => {
(<any>window).__APP_CONFIG__ = config;
const appMountPoint = document.querySelector('[xs-app-mountpoint]');
const upgrade = platformRef.injector.get(UpgradeModule) as UpgradeModule;
upgrade.bootstrap(appMountPoint, [Ng1Module], {strictDi: false});
console.log('hybrid app bootstrapped');
}).catch(err => console.log(err));
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句