升级到Angular 6后,应用程序显示空白屏幕

时间:2018-06-04 05:55:04

标签: angular angular6

我已按照https://update.angular.io/处的步骤进行操作 升级到Angular 6后,应用程序显示所有路径的空白屏幕。并且浏览器控制台中也没有显示错误。每个路由都重定向到默认路径http://localhost:4200。 但http://localhost:4200/admin工作正常。

任何人都可以提供帮助,可能是什么问题? 您需要我从哪些信息中重现问题?

以下是依赖项列表:

"dependencies": {
    "@angular/animations": "6.0.3",
    "@angular/cdk": "^6.2.0",
    "@angular/common": "6.0.3",
    "@angular/compiler": "6.0.3",
    "@angular/core": "6.0.3",
    "@angular/forms": "6.0.3",
    "@angular/http": "6.0.3",
    "@angular/material": "^6.2.0",
    "@angular/material-moment-adapter": "^6.2.0",
    "@angular/platform-browser": "6.0.3",
    "@angular/platform-browser-dynamic": "6.0.3",
    "@angular/router": "6.0.3",
    "@ng-bootstrap/ng-bootstrap": "^2.0.0",
    "@ngx-translate/core": "^10.0.2",
    "@ngx-translate/http-loader": "^3.0.1",
    "@swimlane/ngx-charts": "^8.0.2",
    "@types/lodash": "^4.14.87",
    "bootstrap": "4.0.0",
    "chart.js": "^2.7.2",
    "classlist.js": "^1.1.20150312",
    "codelyzer": "^4.3.0",
    "core-js": "^2.5.1",
    "font-awesome": "^4.7.0",
    "lodash": "^4.17.4",
    "moment": "^2.19.3",
    "ng2-charts": "^1.6.0",
    "ng2-dragula": "^1.3.1",
    "ng2-file-upload": "^1.3.0",
    "ng4-charts": "^1.0.2",
    "rxjs": "^6.2.0",
    "web-animations-js": "^2.2.5",
    "zone.js": "^0.8.19"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.6.6",
    "@angular/cli": "^6.0.7",
    "@angular/compiler-cli": "6.0.3",
    "@types/jasmine": "2.5.38",
    "@types/node": "^6.0.92",
    "jasmine-core": "~2.5.2",
    "jasmine-spec-reporter": "~3.2.0",
    "karma": "~1.4.1",
    "karma-chrome-launcher": "~2.0.0",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^0.2.0",
    "karma-jasmine": "^1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "node-sass": "^4.7.2",
    "protractor": "~5.1.0",
    "ts-node": "~2.0.0",
    "tslint": "~5.10.0",
    "typescript": "^2.7.2"
  }

angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "bcare": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist",
            "index": "src/index.html",
            "main": "src/main.ts",
            "tsConfig": "src/tsconfig.app.json",
            "polyfills": "src/polyfills.ts",
            "assets": [
              "src/assets"
            ],
            "styles": [
              "src/fonts.scss",
              "src/styles.scss"
            ],
            "scripts": []
          },
          "configurations": {
            "prod": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "bcare:build"
          },
          "configurations": {
            "prod": {
              "browserTarget": "bcare:build:prod"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "bcare:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "karmaConfig": "./karma.conf.js",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.spec.json",
            "scripts": [],
            "styles": [
              "src/fonts.scss",
              "src/styles.scss"
            ],
            "assets": [
              "src/assets"
            ]
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "src/tsconfig.app.json",
              "src/tsconfig.spec.json"
            ],
            "exclude": []
          }
        }
      }
    },
    "bcare-e2e": {
      "root": "",
      "sourceRoot": "",
      "projectType": "application",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "./protractor.conf.js",
            "devServerTarget": "bcare:serve"
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "e2e/tsconfig.e2e.json"
            ],
            "exclude": []
          }
        }
      }
    }
  },
  "defaultProject": "bcare",
  "schematics": {
    "@schematics/angular:component": {
      "prefix": "cc",
      "styleext": "scss"
    },
    "@schematics/angular:directive": {
      "prefix": "cc"
    }
  }
}

APP-routing.module.ts

import {RouterModule, Routes} from '@angular/router';
import {NotFoundComponent} from './core/main/not-found.component';
import {HomeComponent} from './core/main/home/home.component';
import {LoginComponent} from './core/main/login/login.component';
import {NgModule} from '@angular/core';
import {HomeGuard} from './core/main/home/home.guard';
import {CanLoadAdmin} from './core/services/can-load-admin';
import {RulesPreviewComponent} from './branches/preparation-admin/main/rules/rules-preview/rules-preview.component';

export const APP_ROUTES: Routes = [
    {
        path: '',
        component: HomeComponent,
        canActivate: [HomeGuard],
        children: [
            {
                path: '',
                redirectTo: 'preparation',
                pathMatch: 'full'
            },
            {
                path: 'preparation',
                loadChildren: 'app/branches/preparation/preparation.module#PreparationModule'
            },
            {
                path: 'admin',
                loadChildren: 'app/admin/admin/admin.module#AdminModule',
                canLoad: [CanLoadAdmin]
            }
        ]
    },
    {path: 'login', component: LoginComponent},
    {path: 'email-preview/:id', component: RulesPreviewComponent},
    {path: '**', component: NotFoundComponent},
];


@NgModule({
    imports: [RouterModule.forRoot(APP_ROUTES)],
    exports: [RouterModule]
})
export class AppRoutingModule {
}

5 个答案:

答案 0 :(得分:5)

从Angular 5更新到6后,我遇到了同样的问题。

我发现Angular根本没有在我的应用程序上加载,我什至没有收到消息“ Angular is running in the development mode. Call enableProdMode() to enable the production mode.”。经过一番挖掘,我发现Angular 6生成的文件之一有所不同。在生成inline.jspolyfills.jsscripts.jsvendor.jsmain.js之前。从Angular 6开始,它现在生成runtime.js而不是inline.js。我一直在MVC中使用Angular,所以我直接引用了输出文件。我将引用从inline.*更改为runtime.*(对于MVC,它在BundlesConfig.cs中),一切都正确了。

除非更改,否则生成的文件应在/dist/中生成。您可以在angular.json下的projects -> my-project-name -> architect -> build -> options -> outputPath中确认路径。

与升级有关的另一个问题可能与您的问题有关,因为ng build的-ec参数不再适用于Angular 6;我的css文件不再生成。您现在必须使用--extract-css。

希望有帮助。

答案 1 :(得分:3)

我遇到了类似角度6的行为。对于之前的版本,当你遇到路由错误时,他们会使用推送状态而不是真正的导航重定向到默认的根目录。

对您来说意味着什么:为了查看错误,请在浏览器控制台中激活“保留日志”(例如,在Chrome中点击齿轮符号并设置复选框),然后重试。错误应该出现,你可以继续解决它们。

答案 2 :(得分:0)

您需要检查您的提供商,因为不需要在app.module文件中包含您的提供商。你可以在控制台中提供一些像你的代码和错误的细节,而不是有用的

答案 3 :(得分:0)

您可能会遇到此错误。您可以看到,如果在浏览器的控制台视图中打开应用程序。

  

NullInjectorError:没有ApplicationRef的提供者!

如果是这样,将BrowserModule导入到 app.module.ts 中将解决您的问题。

import { BrowserModule } from '@angular/platform-browser';

您可以查看相关答案here

答案 4 :(得分:0)

您必须在index.html文件中进行更改: 更改为然后运行ng build --prod