你必须返回可观察对象,而不是订阅它,你也可以使用
tap
以在流内产生副作用。
只有这样
user$
将在应用程序加载之前初始化。
public user$ = new BehaviorSubject<AuthUser | null>(null);
constructor(private readonly http: HttpClient) {}
public getSession() {
return this.http
.get<SessionUser>('https://localhost:7298/api/auth/session', {
withCredentials: true,
})
.pipe(
tap((user) => {
(user as AuthUser).isAuthenticated = true;
this.user$.next(user as AuthUser);
})
);
}
你为什么打电话来
getSessionMock
不应该吗
getSession
.
export const appConfig: ApplicationConfig = {
providers: [
provideZoneChangeDetection({ eventCoalescing: true }),
provideRouter(routes),
provideHttpClient(),
provideAnimationsAsync(),
{
provide: APP_INITIALIZER,
useFactory: (userService: UserService) => {
return () => userService.getSession();
},
deps: [UserService],
multi: true,
},
],
};