【问题标题】:Angular4 routes not working after page refresh页面刷新后Angular4路由不起作用
【发布时间】:2019-02-24 09:17:04
【问题描述】:

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

localhost:4200/dashboard 如果我们刷新此页面,它将转到 localhost:4200/login

我不知道项目发生了什么变化,它突然停止工作。请帮忙。

包.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 serve 时,它​​会显示角度 cli 警告。

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

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

【问题讨论】:

  • 向我们展示您的路线文件。
  • @Exterminator 我更新了代码请检查
  • 准确地定义“不起作用”:你在做什么,你期望发生什么,反而会发生什么。关于您收到的警告,不言自明:您在那句话中不明白什么?
  • NgModule不需要声明
  • @HarshitTailor 刷新浏览器时出现什么错误?没有发现您的路线配置有任何问题

标签: angular angular4-router


【解决方案1】:

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

【讨论】:

  • 感谢您的回复。我已经注释掉 canActivate: [AuthGuard] this line from route 但仍然得到相同的结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-15
  • 2021-06-23
  • 1970-01-01
  • 2020-08-20
  • 2022-12-07
  • 1970-01-01
  • 2014-08-23
相关资源
最近更新 更多