angular 9 candeactivate 不工作虽然代码似乎没问题而且没有抛出任何错误
angular 9 candeactivate not working although code seems to be fine and not throwing any error
大家好,我是 Angular 的新手,正在使用以下教程学习内容 link。由于某种原因, canDeactivate 路由守卫似乎不起作用。任何帮助将不胜感激,因为我尝试检查很多东西但 none 有效。我有最新的 angular CLI,我的代码中没有错误,并且出于某种原因,在路由更改期间根本没有调用 canDeactivate 函数。
我正在 CreateEmployee 路线上应用该功能,因此当我填写 createEmployee 的表格并尝试导航到不同的路线时,它应该会启动。
create-employee-component.html: 在这里,我有几个表单元素
<form #employeeForm = "ngForm" (ngSubmit)="saveEmployee()" [ngClass]="{'was-validated': employeeForm.submitted}" novalidate>
创建员工-component.ts
import { Component, OnInit, ViewChild } from '@angular/core';
import { NgForm } from '@angular/forms';
import { Department } from '../models/department.model';
import { BsDatepickerConfig } from 'ngx-bootstrap/datepicker';
import { Employee } from '../models/employee.model';
import { EmployeeService } from './employee.service';
import { Router } from '@angular/router';
@Component({
selector: 'app-create-employee',
templateUrl: './create-employee.component.html',
styleUrls: ['./create-employee.component.css']
})
export class CreateEmployeeComponent implements OnInit {
@ViewChild('employeeForm') public createEmployeeForm: NgForm;
datePickerConfig: Partial<BsDatepickerConfig>;
previewPhoto = false;
创建员工可以停用-guard.service.ts
import { Injectable } from '@angular/core';
import { CanDeactivate } from '@angular/router';
import { CreateEmployeeComponent } from './create-employee.component';
@Injectable()
export class CreateEmployeeCanDeactivateGuardService implements CanDeactivate<CreateEmployeeComponent>{
canDeactivate(component: CreateEmployeeComponent): boolean{
alert("HJEJJEJEJ");
if(component.createEmployeeForm.dirty)
{
return confirm('Are you sure you want to discard your changes?');
}
return true;
}
}
app-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { ListEmployeesComponent } from './employees/list-employees.component';
import { CreateEmployeeComponent } from './employees/create-employee.component';
import { CreateEmployeeCanDeactivateGuardService } from './employees/create-employee-can-deactivate-guard.service';
const routes: Routes = [
{path: 'list', component: ListEmployeesComponent},
{
path:'create',
component: CreateEmployeeComponent,
canDeactivate: [CreateEmployeeCanDeactivateGuardService]
},
{path: '', redirectTo:'/list', pathMatch:'full'}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
providers: [CreateEmployeeCanDeactivateGuardService]
})
export class AppRoutingModule { }
根据我在 Whosebug 上找到的其他一些答案,我觉得一切都是正确的。请让我知道我在这里做错了什么。我也发布了我的代码 here.
我终于找到问题所在了。经过一段时间的研究后,我发现之前在导航栏中我使用的是带有锚标记的 href 元素,因此 canDeactivate
守卫没有启动。现在我将其更改为 [routerLink]="['/list']"
并且canDeactivate
开始正常工作。
发布答案,因为它可能对正在寻找解决方案的人有用:
带有锚标记和 href
元素的上一个导航栏:
<a class="nav-link" href="list">List <span class="sr-only">(current)</span></a>
使用锚标记和 routerLink
更改了导航栏,它与 canDeactivate
:
一起工作正常
<a class="nav-link" [routerLink]="['/list']">List <span class="sr-only">(current)</span></a>
如果您正在寻找整个代码,请查看我提到的与 canDeactivate
守卫相关的所有代码块的问题。
大家好,我是 Angular 的新手,正在使用以下教程学习内容 link。由于某种原因, canDeactivate 路由守卫似乎不起作用。任何帮助将不胜感激,因为我尝试检查很多东西但 none 有效。我有最新的 angular CLI,我的代码中没有错误,并且出于某种原因,在路由更改期间根本没有调用 canDeactivate 函数。
我正在 CreateEmployee 路线上应用该功能,因此当我填写 createEmployee 的表格并尝试导航到不同的路线时,它应该会启动。
create-employee-component.html: 在这里,我有几个表单元素
<form #employeeForm = "ngForm" (ngSubmit)="saveEmployee()" [ngClass]="{'was-validated': employeeForm.submitted}" novalidate>
创建员工-component.ts
import { Component, OnInit, ViewChild } from '@angular/core';
import { NgForm } from '@angular/forms';
import { Department } from '../models/department.model';
import { BsDatepickerConfig } from 'ngx-bootstrap/datepicker';
import { Employee } from '../models/employee.model';
import { EmployeeService } from './employee.service';
import { Router } from '@angular/router';
@Component({
selector: 'app-create-employee',
templateUrl: './create-employee.component.html',
styleUrls: ['./create-employee.component.css']
})
export class CreateEmployeeComponent implements OnInit {
@ViewChild('employeeForm') public createEmployeeForm: NgForm;
datePickerConfig: Partial<BsDatepickerConfig>;
previewPhoto = false;
创建员工可以停用-guard.service.ts
import { Injectable } from '@angular/core';
import { CanDeactivate } from '@angular/router';
import { CreateEmployeeComponent } from './create-employee.component';
@Injectable()
export class CreateEmployeeCanDeactivateGuardService implements CanDeactivate<CreateEmployeeComponent>{
canDeactivate(component: CreateEmployeeComponent): boolean{
alert("HJEJJEJEJ");
if(component.createEmployeeForm.dirty)
{
return confirm('Are you sure you want to discard your changes?');
}
return true;
}
}
app-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { ListEmployeesComponent } from './employees/list-employees.component';
import { CreateEmployeeComponent } from './employees/create-employee.component';
import { CreateEmployeeCanDeactivateGuardService } from './employees/create-employee-can-deactivate-guard.service';
const routes: Routes = [
{path: 'list', component: ListEmployeesComponent},
{
path:'create',
component: CreateEmployeeComponent,
canDeactivate: [CreateEmployeeCanDeactivateGuardService]
},
{path: '', redirectTo:'/list', pathMatch:'full'}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
providers: [CreateEmployeeCanDeactivateGuardService]
})
export class AppRoutingModule { }
根据我在 Whosebug 上找到的其他一些答案,我觉得一切都是正确的。请让我知道我在这里做错了什么。我也发布了我的代码 here.
我终于找到问题所在了。经过一段时间的研究后,我发现之前在导航栏中我使用的是带有锚标记的 href 元素,因此 canDeactivate
守卫没有启动。现在我将其更改为 [routerLink]="['/list']"
并且canDeactivate
开始正常工作。
发布答案,因为它可能对正在寻找解决方案的人有用:
带有锚标记和 href
元素的上一个导航栏:
<a class="nav-link" href="list">List <span class="sr-only">(current)</span></a>
使用锚标记和 routerLink
更改了导航栏,它与 canDeactivate
:
<a class="nav-link" [routerLink]="['/list']">List <span class="sr-only">(current)</span></a>
如果您正在寻找整个代码,请查看我提到的与 canDeactivate
守卫相关的所有代码块的问题。