Angular 9无法停用,尽管代码看起来不错并且没有引发任何错误

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

您好,我是Angular的新手,请使用下面的教程link学习这些内容。由于某种原因,canDeactivate路由防护似乎无法正常工作。当我尝试检查很多东西但没有任何效果时,任何帮助将不胜感激。我使用的是最新的角度CLI,我的代码中没有错误,并且由于某种原因,在路由更改期间根本没有调用canDeactivate函数。

我正在CreateEmployee路线上应用该功能,因此当我填写createEmployee的表单并尝试导航到其他路线时,它将启动。

create-employee-component.html:在此,我几乎没有表单元素

<form #employeeForm = "ngForm" (ngSubmit)="saveEmployee()" [ngClass]="{'was-validated': employeeForm.submitted}" novalidate>

create-employee-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;

create-employee-can-deactivate-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 { }

根据我在StackOverflow上找到的其他一些答案,我认为一切都是正确的。请让我知道我在做什么错。我还发布了我的代码here

angular-ui-router angular-cli angular-router-guards candeactivate
1个回答
1
投票

我终于发现了问题所在。经过一段时间的研究,我发现以前在导航栏中我使用的是带有锚标记的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防护有关的所有代码块的问题。

© www.soinside.com 2019 - 2024. All rights reserved.