具有动态值的Angular index.html |网络工作者 [英] Angular index.html with dynamic values | web-worker

查看:116
本文介绍了具有动态值的Angular index.html |网络工作者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直到最近,我们还是使用express.js为Angular提供index.html,因为我们需要在应用启动之前从数据库中填充动态变量.

Until recently we used express.js to serve index.html for Angular, because we need to fill in dynamic variables from the database before the app starts.

<script> window .__ envs = {{{json envs}}} </script>

但是,新的Angular 7通过Web-worker缓存了源index.html.

However, the new Angular 7 caches the source index.html through web-worker.

因此,当我加载Web时,只有在单击重新加载后,它才能正确加载.

So when I load the web, it doesn't load properly until I click on the reload.

我试图从Webworker禁用index.html.什么都没发生.

I tried to disable index.html from the webworker. Nothing happened.

我试图关闭网络工作者并将其删除.现在,我不必使用硬重载,但是第一次加载仍将显示源文件,而不是通过express.js修改的文件.

I tried to turn off the web-worker and remove it everywhere. Now I don't have to use a hard reload, but still the first load will show the source file instead of the modified through express.js

1)为什么源代码会加载源文件以及如何获取源文件?

1) Why does the source load the source file and how does it get to it?

2)我可以在Webworker中对其进行设置吗?

2) Can I setup it in a webworker?

推荐答案

感谢您的回答,但没有人能完全解决我的问题. 我不能使用APP_INITIALIZER,因为目前我还不知道API_URL,这是我需要传递给应用程序的内容之一.它是动态值,因此不能成为process.env的一部分.

Thanks for the answers, but not one can solve my problem exactly. I can't use APP_INITIALIZER because I don't know API_URL at the moment and that's one of the things I need to pass to the application. It is dynamic value so it cannot be part of process.env.

对我来说,禁用index.html缓存不正确.

Disabling caching index.html doesn't work properly for me.

但是解决方案非常简单.

我将<script src="/envs">放入其中,而不是直接更改index.html文件 然后我在express.js中公开了这个文件

Instead of changing the index.html file directly, I put <script src="/envs"> into it and I exposed this file in express.js

app.get('/envs', async (req, res) => {
    const envs = ...
    res.end('window.__envs = ' + JSON.stringify(envs))
})

这篇关于具有动态值的Angular index.html |网络工作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆