Khi tôi mở ứng dụng Angular 2 trong cửa sổ trình duyệt mới, tôi nhận được 10 mục nhập trong lịch sử trang trình duyệt. Điều này xảy ra trên bất kỳ trang liệt kê trong các bộ định tuyến và không có trang nào đó (ví dụ http://localhost:8080 hoặc http://localhost:8080/survey vv)Góc 2 - nhiều mục lịch sử khi tải trang
const routes: Routes = [
{path: 'survey', component: SurveyComponent, canActivate: [AuthGuard]},
{path: 'survey/:id', component: SurveyComponent, canActivate: [AuthGuard]},
{path: '', component: HomeComponent},
{path: 'about', component: AboutComponent},
{path: 'terms', component: TermsAndConditionsComponent},
{path: 'map', component: MapComponent},
{path: 'what-next', component: WhatNextComponent},
{path: 'contact', component: ContactComponent},
{path: '**', redirectTo: '', pathMatch: 'full'},
];
Tôi đang sử dụng bộ định tuyến 3.1.2
"@angular/router": "3.1.2",
Tôi có tìm thấy một số similar question từ tháng 3 năm ngoái nhưng câu trả lời cho rằng nó đã được giải mã trong mã Angular rồi.
Cập nhật Theo yêu cầu, đây là mã AuthGuard
import {Injectable} from '@angular/core';
import {AuthenticationService} from '../_services/index';
import {LoginModalService} from "../login_modal/login_modal.service";
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router';
import {Observable} from 'rxjs/Observable';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private authentication: AuthenticationService, private loginModalService: LoginModalService) {
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if (this.authentication.isAuthenticated()) {
return Observable.of(true);
} else {
this.loginModalService.toggleVisable(state.url);
return Observable.of(false);
}
}
}
Tôi nghĩ rằng tôi cần một mẫu để xem qua, tôi chưa từng thấy điều này trong bất kỳ ứng dụng Góc nào của mình, vì vậy tôi không chắc chắn cách tạo lại sự cố này ... – MichaelSolati
Nếu chúng tôi có thể xem các thành phần của bạn được liệt kê ở đó, sẽ dễ dàng hơn đưa ra một giải pháp hoặc ít nhất là một đầu mối, chỉ nghĩ rằng tôi có thể gợi ý ngay bây giờ là lắng nghe sự kiện tuyến đường, kiểm tra câu trả lời này https://stackoverflow.com/questions/33520043/how-to-detect-a- route-change-in-angular-2/38080657 # 38080657 –
bạn đã thử cập nhật lên 4.2.0 chưa? –