刷新后的 Angular 6 应用程序重定向到根目录



我的 angular 6 应用程序有问题,刷新后它会回到根目录。我发现问题在哪里,但我不知道如何更改或添加代码。

import { Component, OnInit } from "@angular/core";
import * as firebase from "firebase";
import { Router } from "@angular/router";
import { UserService } from "../../service/user.service";
@Component({
selector: "app-navbar",
templateUrl: "./navbar.component.html",
styleUrls: ["./navbar.component.css"]
})
export class NavbarComponent implements OnInit {
isLoggedIn: boolean = false;
name: string;
email: string;
uid: string;
spec: string;
constructor(private userService: UserService, private router: Router) {}
ngOnInit() {
this.userService.statusChange.subscribe(userData => {
if (userData) {
this.name = userData.name;
this.email = userData.email;
this.uid = userData.uid;
this.spec = userData.spec;
} else {
this.name = null;
this.email = null;
this.uid = null;
this.spec = null;
}
});
firebase.auth().onAuthStateChanged(userData => {
if (userData) {
this.isLoggedIn = true;
console.log("user is login");
const user = this.userService.getProfile(); 
if (user && user.name) {
this.name = user.name;
this.email = user.email;
this.uid = user.uid;
this.spec = user.spec;
}
this.router.navigate(["/"]);// **REFRESH PROBLEM**
} else {
this.isLoggedIn = false;
console.log("user is logout");
this.router.navigate(["/login"]);
}
});
}
onlogout() {
firebase
.auth()
.signOut()
.then(() => {
this.userService.remove();
this.isLoggedIn = false;
});
}
}

例如,如果我在this.router.navigate(["/"](中添加我的一些路由,它将始终重定向到该新根,但是如果我全部删除,它将再次返回到root。也许本地存储可以提供帮助,但我不知道如何实现:(

更新的路由模块

RouterModule.forRoot([
{
path: "",
component: DashboardComponent,
canActivate: [AuthGuardService]
},
{ path: "login", component: LoginComponent },
{ path: "register", component: RegisterComponent },
{
path: "pacijent/:id",
component: PacijentComponent,
canActivate: [AuthGuardService]
},
{
path: "pacijent/:id/edit",
component: PacijentEditComponent,
canActivate: [AuthGuardService]
},
{
path: "novi-pacijent",
component: NoviPacijentComponent,
canActivate: [AuthGuardService]
},
{
path: "istorija/:id/:id",
component: NalazComponent,
canActivate: [AuthGuardService]
},
{ path: "**", component: NotfoundComponent }
])
],

"/"总会带你到根源。

提到你想降落的正确路径。

前任:

this.router.navigate(["/dashboard"])

如果您想在刷新后进入当前页面,请使用this.router.url

您可以将this.router.navigate(["/"]);替换为this.router.navigate([this.router.url]);

你能做的最好的事情就是留在当前的路线上

this.router.navigate([""]);

将重新加载路由,但您必须将重用路由策略设置为 false 才能强制重新加载路由。

如果要随之传递任何查询参数

let params = ...; // define query params
this.router.navigate([""], params);

最新更新