页面刷新后Angular4路由不起作用

时间:2018-09-20 06:13:37

标签: angular angular4-router

刷新页面后,Angular 4路由不起作用。它曾经工作过。 当前,当页面刷新路由要登录页面(默认路由)时,它应该停留在同一路由上。

localhost:4200 / dashboard (如果刷新页面),它将转到 localhost:4200 / login

我不知道项目中突然停止了什么更改。请帮忙。

Package.json

{
  "name": "Projectname",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build --prod",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^5.0.0",
    "@angular/common": "^5.0.0",
    "@angular/compiler": "^5.0.0",
    "@angular/core": "^5.0.0",
    "@angular/forms": "^5.0.0",
    "@angular/http": "^5.0.0",
    "@angular/platform-browser": "^5.0.0",
    "@angular/platform-browser-dynamic": "^5.0.0",
    "@angular/router": "^5.0.0",
    "@aspnet/signalr-client": "^1.0.0-alpha2-final",
    "@types/signalr": "^2.2.35",
    "angular": "^1.6.9",
    "angular-datatables": "^5.0.0",
    "angular-localstorage": "^1.1.5",
    "angular-rateit": "^4.0.2",
    "angular-sortablejs": "^2.5.1",
    "angular-star-rating": "^3.0.8",
    "angular2-draggable": "^1.1.0-beta.0",
    "angular2-focus": "^1.1.1",
    "angular4-color-picker": "^1.4.2",
    "angular4-files-upload": "^1.0.2",
    "async": "^2.6.0",
    "auth0": "^2.9.1",
    "auth0-js": "^9.4.1",
    "core-js": "^2.4.1",
    "datatables.net": "^1.10.16",
    "datatables.net-dt": "^1.10.16",
    "datatables.net-responsive": "^2.2.1",
    "datatables.net-responsive-dt": "^2.2.1",
    "lodash": "^4.8.0",
    "ng-file-upload": "^12.2.13",
    "ng2-dnd": "^5.0.2",
    "ng2-file-upload": "^1.3.0",
    "ng2-toastr": "^4.1.2",
    "ngx-ckeditor": "^0.1.1",
    "ngx-color-picker": "^5.3.0",
    "ngx-drag-drop": "^1.0.3",
    "ngx-editor": "^3.2.1",
    "ngx-file-drop": "^2.0.2",
    "ngx-loading": "^1.0.14",
    "ngx-rating": "0.0.9",
    "ngx-toastr": "^8.1.0",
    "ngx-uploader": "^4.2.2",
    "rxjs": "^5.5.2",
    "signalr": "^2.2.3",
    "sortablejs": "^1.7.0",
    "zone.js": "^0.8.14"
  },
  "devDependencies": {
    "@angular/cli": "1.7.3",
    "@angular/compiler-cli": "^5.0.0",
    "@angular/language-service": "^5.0.0",
    "@types/datatables.net": "^1.10.9",
    "@types/jasmine": "~2.5.53",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~6.0.60",
    "codelyzer": "^4.0.1",
    "jasmine-core": "~2.6.2",
    "jasmine-spec-reporter": "~4.1.0",
    "karma": "~1.7.0",
    "karma-chrome-launcher": "~2.1.1",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~3.2.0",
    "tslint": "~5.7.0",
    "typescript": "~2.4.2"
  }
}

路由文件

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { LoginComponent } from './login/login.component';
import { DashboardComponent } from './dashboard-components/dashboard/dashboard.component';
import { AuthGuard } from './auth-guard.service';
import { ResetPasswordComponent } from './reset-password/reset-password.component';
import { createTemplateData } from '@angular/core/src/view/refs';
import { ErrorPageComponent } from './components/error-page/error-page.component';
import { UserLogComponent } from './Views/user-log/user-log.component';

const routes: Routes = [
  {
    path: '',
    redirectTo: '/login',
    pathMatch: 'full'
  },
  {
    path: 'login',
    component: LoginComponent
    //canLogIn:[AuthGuard]
  },
  {
    path: 'dashboard',
    component: DashboardComponent,
    canActivate: [AuthGuard]
  },
  {
    path: 'reset-password',
    component: ResetPasswordComponent
  },
  {
    path: '404',
    component: ErrorPageComponent
  },
  {
    path:'logs',
    component:UserLogComponent,
    canActivate: [AuthGuard]
  }
];

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

当我使用ng服务时,还有另一件事是显示角度cli警告。

您的全局Angular CLI版本(1.7.4)大于本地版本 版本(1.7.3)。使用本地Angular CLI版本。

要禁用此警告,请使用“ ng set --global warnings.versionMismatch = false”。

1 个答案:

答案 0 :(得分:3)

1) 您的authguard会将您重定向到登录名,因为您没有(立即)在刷新时登录。

2)关于CLI警告

在您的devDependencies中,您具有:

"@angular/cli": "1.7.3",

在全球范围内,您已安装@ angular / cli @ 1.7.4 这就是CLI抱怨的原因。

如果要在应用程序中保留@ angular / cli @ 1.7.3并避免出现警告,则可以执行提及的操作:

ng set --global warnings.versionMismatch=false 

或在全球安装相同版本

npm i -g @angular/cli@1.7.3