angular - I have this error: this.authService.isAuthenticated is not a function
问题描述
I am a beginner of angular ionic framework. I have error on my login page I have attached console screen shot.
I create this pages with below youtube video (in that video 15:55) that guy also getting same error but I dont know how that guy solved that error please anyone help to solve this.
youtube link: https://www.youtube.com/watch?v=z3pDqnuyzZ4
member-routing.module.ts
import { NgModule } from '@angular/core';
import { PreloadAllModules, RouterModule, Routes } from '@angular/router';
const routes: Routes = [
{ path: 'dashboard', loadChildren: './dashboard/dashboard.module#DashboardPageModule' },
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class MemberRoutingModule { }
app-routing.module.ts
import { NgModule } from '@angular/core';
import { PreloadAllModules, RouterModule, Routes } from '@angular/router';
import { AuthGuardService } from './services/auth-guard.service';
const routes: Routes = [
{ path: '', redirectTo: 'login', pathMatch: 'full' },
{ path: 'login', loadChildren: './public/login/login.module#LoginPageModule' },
{ path: 'members',canActivate:[AuthGuardService],loadChildren:'./members/member-routing.module#MemberRoutingModule' }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
authentication service.ts
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { Storage } from '@ionic/storage';
import { Platform } from '@ionic/angular';
const TOKEN_KEY = 'auth-token';
@Injectable({
providedIn: 'root'
})
export class AuthenticationService {
authenticationState = new BehaviorSubject(false);
constructor(private Storage: Storage, private plt: Platform) {
this.plt.ready().then(()=>{
this.checkToken();
});
}
login(){
return this.Storage.set(TOKEN_KEY,'key 12345').then(res=>{
this.authenticationState.next(true);
});
}
logout(){
return this.Storage.remove(TOKEN_KEY).then(()=>{
this.authenticationState.next(false);
});
}
isAuthenticated(){
return this.authenticationState.value;
}
checkToken(){
return this.Storage.get(TOKEN_KEY).then(res=>{
if(res){
this.authenticationState.next(true);
}
});
}
}
auth-guard.service.ts
import { Injectable } from '@angular/core';
import { CanActivate } from '@angular/router';
import { AuthenticationService } from './authentication.service';
@Injectable({
providedIn: 'root'
})
export class AuthGuardService implements CanActivate{
constructor(private authService: AuthenticationService) { }
canActivate(): boolean{
return this.authService.isAuthenticated();
}
}
解决方案
您必须生成 Guard not 服务
import { Injectable } from '@angular/core';
import {
Router,
CanActivate,
ActivatedRouteSnapshot,
RouterStateSnapshot
} from '@angular/router';
import { SessionService } from './session.service';
@Injectable({
providedIn: 'root'
})
export class AfterLoginGuard implements CanActivate {
constructor(private router: Router, private session: SessionService) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
const currentUser = this.session.currentUserValue;
if (currentUser) {
// authorized so return true
return true;
}
// not logged in so redirect to login page with the return url
this.router.navigate(['/admin/login'], {
queryParams: { returnUrl: state.url }
});
return false;
}
}
像这样。你生成一个服务而不是守卫。
推荐阅读
- java - ImageButton 不起作用 Android Studio Java
- javascript - Next.js - 服务器上的样式化组件样式化中断
- json - 从 Shopify API 获取 line_items 到 Google 表格中
- akamai - 将 Akamai 属性导入 Pulumi 时输入验证失败
- android - 使用 API 30 通过 PDF.js 加载 PDF
- python - 试图迭代字典中的浮点数
- plot - 使用平滑选项绘制曲线的问题
- android - 如何在扩展属性上添加 lateinit var
- c++ - 将数据存储到数组中
- r - 给定最大因数的总公因数