我在17号角创建了一个功能性后卫
import { Inject } from '@angular/core';
export const checkoutGuard: CanActivateFn = (route, state) => {
const customerService = Inject(CustomerService);
const router = Inject(Router);
if(customerService.isAuthenticated()) {
return true;
} else {
return router.navigateByUrl('/login');
}
};
我得到错误CustomerService.isAuthenticated不是一个函数.
我在app.config.ts providers: [provideRouter(routes), provideHttpClient(), CustomerService,]
中声明了该服务
这就是第II号路由在{path:'checkout', component:CheckoutComponent, canActivate:[checkoutGuard]}
号路由中宣布的
这是服务等级
@Injectable({ providedIn: 'root' })
export class CustomerService {
isAuthenticated():boolean { return true; }
}