为什么Angular组件无法在浏览器后退/前进导航中加载?

时间:2019-04-25 00:24:36

标签: angular angular-components angular-router

角度7.2.13

使用浏览器后退按钮导航时,将加载组件的HTML,但未加载组件css / js,并且页面将挂起。

但是,通过链接单击或router.navigateByUrl()导航或重新加载页面时,加载工作正常。但是使用前进/后退按钮导航时绝对不会。

我没有控制台错误。

它在开发服务器http://localhost:4200/上完美运行。此问题仅在生产中出现。

更新

这是在子路线中发生的。

更新2

无法在具有相同路由结构的Stackblitz中复制问题。 https://angular-tpsr5z.stackblitz.io/

更新3

当它挂起时,会同时显示以前和新的路线html。

像这样挂起之后,如果我单击任何触发组件方法或更改组件属性的组件,组件将立即加载。

更新4

打包Json

{
  "name": "tuilder-ng",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^7.2.13",
    "@angular/cdk": "^7.3.7",
    "@angular/common": "^7.2.13",
    "@angular/compiler": "^7.2.13",
    "@angular/core": "^7.2.13",
    "@angular/forms": "^7.2.13",
    "@angular/http": "^7.2.13",
    "@angular/material": "^7.3.7",
    "@angular/platform-browser": "^7.2.13",
    "@angular/platform-browser-dynamic": "^7.2.13",
    "@angular/router": "^7.2.13",
    "@ecodev/fab-speed-dial": "^3.1.0",
    "@google/markerclustererplus": "^2.1.11",
    "@swimlane/ngx-charts": "^10.1.0",
    "@types/googlemaps": "^3.30.19",
    "@types/lodash": "^4.14.123",
    "chart.js": "^2.8.0",
    "core-js": "^2.6.5",
    "lodash": "^4.17.11",
    "moment-timezone": "^0.5.23",
    "ng2-charts": "^1.6.0",
    "ngx-material-timepicker": "^2.13.0",
    "ngx-order-pipe": "^2.0.2",
    "rxjs": "^6.4.0",
    "thenby": "^1.3.0",
    "zone.js": "^0.8.29"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^0.13.8",
    "@angular/cli": "^7.3.8",
    "@angular/compiler-cli": "^7.2.13",
    "@angular/language-service": "^7.2.13",
    "@types/jasmine": "^3.3.12",
    "@types/jasminewd2": "~2.0.6",
    "@types/node": "^10.12.30",
    "codelyzer": "~4.5.0",
    "jasmine-core": "~3.3.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "^4.1.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "^2.0.5",
    "karma-jasmine": "~2.0.1",
    "karma-jasmine-html-reporter": "^1.4.0",
    "protractor": "^5.4.2",
    "ts-node": "^8.0.3",
    "tslint": "~5.12.1",
    "typescript": "3.2.4"
  }
}

服务器重定向.htaccess

RewriteEngine On

RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Options +FollowSymLinks
DirectoryIndex index.html

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[a-zA-Z0-9-_.\/]+$ index.html

更新5

减少该问题的演示:https://ngtest.emanweb.design/

1 个答案:

答案 0 :(得分:7)

通过为该特定域关闭Nginx中的ModPagespeed解决了该问题。

$ git add <tab>
src/main/java/com/A.java
src/main/java/com/B.java

ModPagespeed正在与Angular进行某种交互以导致这种奇怪的行为。

或者,可以通过附加以下内容来关闭每个网址的页面速度:pagespeed Disallow "https://*example.com/*";