Angular 6应用程序刷新后重定向到根目录
我的angular 6应用程序有问题,刷新后返回到root。我发现问题出在哪里,但我不知道如何更改或添加代码Angular 6应用程序刷新后重定向到根目录,angular,angular-ui-router,angular6,router,Angular,Angular Ui Router,Angular6,Router,我的angular 6应用程序有问题,刷新后返回到root。我发现问题出在哪里,但我不知道如何更改或添加代码 import { Component, OnInit } from "@angular/core"; import * as firebase from "firebase"; import { Router } from "@angular/router"; import { UserService } from "
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;
});
}
}
例如,如果我添加此.router.navigate([“/”])我的一些路由,它将始终重定向到新的根目录,但如果我全部删除,它将再次返回根目录。本地存储可能会有所帮助,但我不知道如何实施:(
已更新
路由模块
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.url
您可以替换
this.router.navigate([“/”]);
到this.router.navigate([this.router.url]);
您所能做的最好的事情就是保持当前路线的正确位置
this.router.navigate([""]);
将重新加载路由,但必须将reuseroute策略设置为false才能强制重新加载路由
如果要传递任何查询参数,请同时传递
let params = ...; // define query params
this.router.navigate([""], params);
你能在此处提供路由模块和app.module文件代码吗?NP,添加,你可以检查。但我想在刷新后保留当前url。然后尝试
this.router.navigate([“/”])
同样的事情:(你能解释为什么是这个.router.navigate([“/”]));必需。@ViktorMolnarIt将指向当前路由。看起来代码中存在错误的实现。顺便问一下,为什么要刷新?
let params = ...; // define query params
this.router.navigate([""], params);