我正在创建一个服务来检查用户是否登录。这只是一个从后端传递的全局变量“userIsLoggedIn”。该服务将在多个应用程序中使用,有时 userIsLoggedIn 全局变量将不存在。
我所拥有的是...
import { Injectable } from '@angular/core';
declare const userIsLoggedIn: boolean;
@Injectable()
export class UserLoggedInService{
isUserLoggedIn(): boolean {
return userIsLoggedIn || false;
}
}
Run Code Online (Sandbox Code Playgroud)
如果 userIsLoggedIn 全局变量不存在,则会收到错误,如果省略声明行,则会收到错误。
我曾希望能够做类似的事情
declare const userIsLoggedIn?: boolean;
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用。有人可以帮我吗?