firebase Angular 击发7可以激活

t98cgbkg  于 2023-02-25  发布在  Angular
关注(0)|答案(1)|浏览(121)

我在初始化一个新的应用程序角13和角火7。
当使用AngularFireAuthGuard时,我得到了nullInjetor错误

import { AngularFireAuthGuard } from '@angular/fire/compat/auth-guard';

我把它改成了AuthGuard,一切都很好用

import { AuthGuard } from '@angular/fire/auth-guard';

这是有效和好的方式吗?或者我必须提供任何东西来使用AngularFireAuthGuard!!
有效的导入:

import {redirectLoggedInTo,redirectUnauthorizedTo,} from '@angular/fire/compat/auth-guard';
import { AuthGuard } from '@angular/fire/auth-guard';
n6lpvg4x

n6lpvg4x1#

尝试使用**...可以激活**

import { canActivate } from '@angular/fire/compat/auth-guard';

const adminOnly = () => hasCustomClaim('admin');
const redirectUnauthorizedToLogin = () => redirectUnauthorizedTo(['login']);
const redirectLoggedInToItems = () => redirectLoggedInTo(['items']);
const belongsToAccount = (next) => hasCustomClaim(`account-${next.params.id}`);

export const routes: Routes = [
    { path: '',             component: AppComponent },
    { path: 'login',        component: LoginComponent,    ...canActivate(redirectLoggedInToItems) },
    { path: 'items',        component: ItemListComponent, ...canActivate(redirectUnauthorizedToLogin) },
    { path: 'admin',        component: AdminComponent,    ...canActivate(adminOnly) },
    { path: 'accounts/:id', component: AdminComponent,    ...canActivate(belongsToAccount) }
];

我通过此链接AngularFire GitHub docs从AngularFire文档中获得了解决方案

相关问题