我正在尝试使用 ng build 来使用“http-server”在生产中部署我的应用程序。
所以我运行了 ng build,然后使用 http-server 为应用程序提供服务。登录后一切正常,我可以访问每个页面。但是,当我刷新页面或单击浏览器的 URL 栏并按 Enter 键时,我收到“404 Not Found”。
使用“npm start”(ng serve)为我的应用程序提供服务时,我没有遇到此问题。
相关包:Angular 5.1.1、AngularCli 1.6.1
我有一个具有公共和私人方面的应用程序。私有端在认证成功后延迟加载。我有大多数模块都使用的标准核心和共享模块。我也有共享功能模块,这些模块仅由延迟加载的应用程序的私有端使用。
共享功能模块不可避免地进入公共块,因为它们被其他模块引用。共享模块也进入该块,因为它在任何地方都被引用。因为公共块包含共享模块并且共享模块被公共端引用,所以它在应用程序第一次加载时加载。因此,包含在公共块中的私有共享功能模块也在第一页加载时加载,而不是在加载使用它们的私有延迟加载功能时加载。
有没有办法使用 Angular CLI 创建多个最小的公共块?至少我想要一个公共公共块和一个私有公共块。
遵循以下步骤:
定义 Dockerfile:
FROM node:alpine
RUN yarn global add @angular/cli
RUN yarn global add node-sass
RUN mkdir /volumes
WORKDIR /volumes
EXPOSE 4200
ENTRYPOINT ["ng"]
Run Code Online (Sandbox Code Playgroud)从这个 Dockerfile 构建一个镜像:
docker build -t my_angular_image .
Run Code Online (Sandbox Code Playgroud)使用图像创建一个新的 angular 应用程序:
// Create the new app
docker run --rm --mount type=bind,src=$PWD,dst=/volumes my_angular_image new my-app --directory app --style scss
// Change ownership of the generated app
sudo chown -R $USER:$USER .
Run Code Online (Sandbox Code Playgroud)根据镜像,运行一个容器绑定挂载应用程序卷:
docker run -p 4200:4200 --mount type=bind,src=$PWD/app,dst=/volumes my_angular_image serve --host 0.0.0.0
Run Code Online (Sandbox Code Playgroud)结果:
第一次编译按预期工作,容器为应用程序提供服务。但是,当更改必须ng serve在容器中监视的文件的值(来自主机)时,不会触发新的角度构建(因此,不会更新服务的应用程序)。 …
如何每分钟轮询服务器以检查 Service Worker 更新?
我使用了 SWUpdate angular 的可用方法,但它不轮询服务器,而是在页面刷新时获取更新和缓存,这不是必需的。
我遇到了 checkForUpdate() 方法,它非常相似,但不确定它是如何工作的?
任何帮助将不胜感激。
ngOnInit(){
if(this.swUpdate.isEnabled){
this.swUpdate.available.subscribe( () => {
if(confirm("New Version available.. Load?")){
window.location.reload();
}
})
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码在第二次重新加载时从缓存中获取数据。
service-worker progressive-web-apps angular-cli angular angular-service-worker
有人可以解释 angular 5 中延迟加载模块和动态组件加载之间的区别吗?我很难弄清楚何时使用什么。您能否为每个用例提供一个用例,以便我更好地理解它们?我对 angular 5 还很陌生……我知道延迟加载是关于模块的,而动态组件加载是关于组件的。
我正在使用 ngx-bootstrap,我需要将一个 css 文件导入到我的组件中,如下所示:
如果您正在使用 Angular CLI 之类的软件包捆绑器,请从 node_modules/ngx-bootstrap/datepicker/bs-datepicker.css 加载它。
(这是在组件的文档中)。
我不知道如何通过包 bundler 加载 bs-datepicker.css。
我尝试过的是:
styleUrls: ['node_modules/ngx-bootstrap/datepicker/bs-datepicker.css']
(没有成功,没有找到)。
该文件在此目录中:
C:\DEV\senai-5s-webapp\node_modules\ngx-bootstrap\datepicker\bs-datepicker.css
我正在使用带有 aot=true 和 --prod 的 angular cli 构建应用程序,因此应用程序使用 aot 进行编译并且包大小更小,但是我如何以 aot 模式引导应用程序,以便引导程序更快,如本文所述?
当前代码:
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { environment } from './environments/environment';
import { AppModule } from './app/app.module';
if (!environment.local) {
enableProdMode();
}
platformBrowserDynamic().bootstrapModule(AppModule);
Run Code Online (Sandbox Code Playgroud)
文章提出的代码:
import { platformBrowser } from '@angular/platform-browser';
import { enableProdMode } from '@angular/core';
import { environment } from './environments/environment';
//** where is the following file generated by angular-cli? **
import { AppModuleNgFactory } from './app/app.module.ngfactory'; …Run Code Online (Sandbox Code Playgroud) 我正在构建一个打包在 WAR 静态资源中的 Java Web 应用程序。这些静态资源是通过 Angular-CLI 构建的。
Maven 构建通过 Eirslettmaven-frontend-plugin使用 npm 脚本和 npm mojo触发 ng 构建。
问题是,我想base href根据 Maven 构建参数使用自定义,但我没有设法通过环境变量或参数将它传递给 ng。
有人能告诉我如何从 Maven 将参数传递给 ng 构建吗?
代码:
pom.xml
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<executions>
<execution>
<id>node install</id>
<phase>generate-resources</phase>
<goals>
<goal>install-node-and-npm</goal>
</goals>
</execution>
<execution>
<id>npm install</id>
<phase>generate-resources</phase>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>--registry=${npm.internal.registry} --userconfig ${basedir}/.jenkins-npmrc install</arguments>
</configuration>
</execution>
<execution>
<id>npm build</id>
<phase>prepare-package</phase>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>--userconfig ${basedir}/.jenkins-npmrc run-script build</arguments>
<environmentVariables>
<test>this-does-not-work</test>
</environmentVariables>
</configuration>
</execution>
<execution>
<id>npm test</id>
<phase>test</phase> …Run Code Online (Sandbox Code Playgroud) 我以为我已经成功地缓存破坏了 angular-cli 的--output-hashing选项设置all为确保用户获得最新的块/更新。我意识到这并不总是适用于延迟加载的模块。
如果我只对延迟加载模块 + 构建 + 部署到 IIS进行更改,并转到属于延迟加载模块的 URL,那么我会按预期获得最新更改。
但是,如果我关闭并重新打开浏览器并转到不属于延迟加载模块的 URL,然后单击将我带到延迟加载模块的链接,则不会观察到最新更改。
几乎就像浏览器假设每当我从未更改的模块进入应用程序时没有任何更改,并且稍后在我访问已更改的模块时不会选择新块。
更奇怪的是,当我尝试复制问题以提供一些屏幕截图时,问题就消失了。
有没有其他人遇到过使用输出哈希与缓存破坏不一致的情况?
编辑:
进一步调查后,事实证明,如果我转到我网站的着陆页 [IP 地址]:[端口],那么 index.html 来自缓存。
相反,如果我转到任何其他路由 [IP 地址]:[端口]/[路由],那么 index.html 来自服务器。
由于 index.html 指向 inline.(hash).bundle.js,后者又指向延迟加载的模块,因此获取 index.html 的旧副本会导致获取旧版本的 inline.js 和其他模块。
我尝试添加<meta http-equiv="expires" content="0" />到 index.html 并清除浏览器缓存,但仍然从缓存中获取文件。
我也尝试在 web.config 中添加一个部分,但这也不起作用。
如何确保 index.html 始终来自服务器?
angular-cli ×10
angular ×8
angular2-aot ×1
angular5 ×1
debugging ×1
docker ×1
dockerfile ×1
javascript ×1
maven ×1
typescript ×1
webpack ×1