Angular 在向firestore数据库添加数据时防止角度路由重定向到登录页
我正在我的angular应用程序中使用cloud firestore sdk。我已设置对用户数据值更改()的订阅。 我添加了一个方法,用户可以通过该方法相互跟踪。在后端,一切正常。但是,只要我点击follow按钮,路线就会被重定向到登录页 我希望用户保持在同一页面中,从那里单击按钮 app.component.ts->Angular 在向firestore数据库添加数据时防止角度路由重定向到登录页,angular,firebase,google-cloud-firestore,routes,Angular,Firebase,Google Cloud Firestore,Routes,我正在我的angular应用程序中使用cloud firestore sdk。我已设置对用户数据值更改()的订阅。 我添加了一个方法,用户可以通过该方法相互跟踪。在后端,一切正常。但是,只要我点击follow按钮,路线就会被重定向到登录页 我希望用户保持在同一页面中,从那里单击按钮 app.component.ts-> ngOnInit() { this.authService.initAuthStateListener(); } 授权服务-> initAuthStateListe
ngOnInit() {
this.authService.initAuthStateListener();
}
授权服务->
initAuthStateListener(){
this.loadStateChanged.next(true);
return this.afAuth.authState
.subscribe(
user=>{
if(user){
this.isAuthenticated =true;
this.authChange.next(true);
this.authSubs.push(
this.userService.fetchUserDetails(user.uid).subscribe(
user=>{
this.router.navigate(['/training']);
}
));
}
else{
this.cancelAllSubs();
this.isAuthenticated = false;
this.authChange.next(false);
this.router.navigate(['']);
}
}
);
}
.ts页面->
onFollow(){
this.userService.followUser(this.user.userid,this.user.email);
}
用户服务->
userData = new BehaviorSubject<User>(null);
private user:User;
fetchUserDetails(uid:string){
return this.db.collection("Users").doc(uid).valueChanges()
.pipe(tap((user:User)=>{
this.user = user;
console.log(this.user);
this.userData.next({...this.user});
}));
}
followUser(uid:string,email:string){
this.db.collection('Users').doc(this.user.userid)
.update({
following:firebase.firestore.FieldValue.arrayUnion(
{'uid':uid,'email':email})
});
this.db.collection('Users').doc(uid)
.update({
followers:firebase.firestore.FieldValue.arrayUnion(
{'uid':this.user.userid,'email':this.user.email})
});
}
getUserData(){
return ({...this.user});
}
userData=newbehaviorsubject(null);
私人用户:用户;
fetchUserDetails(uid:string){
返回此.db.collection(“Users”).doc(uid).valueChanges()
.管道(水龙头((用户:用户)=>{
this.user=用户;
console.log(this.user);
this.userData.next({…this.user});
}));
}
followUser(uid:string,email:string){
this.db.collection('Users').doc(this.user.userid)
.更新({
以下内容:firebase.firestore.FieldValue.arrayUnion(
{'uid':uid,'email':email})
});
此.db.collection('Users').doc(uid)
.更新({
追随者:firebase.firestore.FieldValue.arrayUnion(
{'uid':this.user.userid,'email':this.user.email})
});
}
getUserData(){
返回({…this.user});
}
“training”是用户登录时我的着陆路线。在AppComponent中,我添加了一个检查来验证用户令牌是否存在,并基于该用户被重定向到training to null路由。
但是,似乎每次用户数据发生变化时,它都会将我重定向回训练(着陆)路线。由于提供了代码,与路线无关,请检查您的HTML按钮,它可能有一个
routerLink
或href
将您引导到着陆页面,如果没有,
检查您的
initAuthStateListener
函数,可能用户未通过身份验证,因此在您编写this.router.navigate(['')时,它会将您引导到主页
,无论如何,唯一可以将您引导到登录页的代码是在您的initAuthStateListener
函数中。您需要发布一些代码。添加了代码。谢谢。不,我的按钮中没有重定向。遵循检查您的initAuthStateListener
函数,可能用户未经过身份验证,因此在编写this.router.navigate(['')时,它会将您引导到主页代码>成功登录后,我有三个不同的路线,作为登录页培训,一个用户路线,在那里我可以看到用户列表和一个详细路线,在那里我可以看到所选用户的详细信息,并单击“跟踪”按钮跟踪它们。只要我点击follow按钮,追随者和追随者的列表就会更新,我会被重定向到培训路线。现在我明白了,路线是在这里发生的`this.router.navigate(['/training']);`因此,如果您想保持在同一页中,请删除上面的代码行