页面刷新后Angular 4路由不起作用

问题描述 投票:0回答:1

页面刷新后Angular 4路由不起作用。它以前工作过。目前,当页面刷新路由进入登录页面(默认路由)时,它应该保持在同一路由上。

localhost:4200 /仪表板,如果我们刷新此页面,它将转到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”。

angular angular4-router
1个回答
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/[email protected]
© www.soinside.com 2019 - 2024. All rights reserved.