【发布时间】:2020-05-21 23:51:56
【问题描述】:
我在我的应用程序中开发了一个 cookie 检查器服务,它应该检查我的 cookie 同意弹出窗口的状态:
@Injectable()
export class CookieCheckService implements OnInit, OnDestroy {
public hasConsented = false;
private cookieStatusChangeSubscription: Subscription;
constructor(
private ccService: NgcCookieConsentService
) {
}
ngOnInit() {
if (this.ccService.hasConsented()) {
this.hasConsented = true;
}
console.log(this.hasConsented);
this.cookieStatusChangeSubscription = this.ccService.statusChange$.subscribe(
(event: NgcStatusChangeEvent) => {
this.hasConsented = event.status === this.ccService.getStatus().allow;
});
}
ngOnDestroy() {
this.cookieStatusChangeSubscription.unsubscribe();
}
}
我的想法是现在从我需要检查状态的任何组件中调用它,例如在我显示 Google 地图的页脚中:
@Component({
selector : 'app-footer',
templateUrl: './footer.component.html',
styleUrls : ['./footer.component.css']
})
export class FooterComponent {
hasConsented = false;
constructor(
private cookieCheckService: CookieCheckService
) {
this.hasConsented = cookieCheckService.hasConsented;
}
}
通常,当我现在按允许时,我想通过ngIf 使我的 Google 地图小部件可见,但不知何故,我没有从我的服务中获得任何价值 - 最初也是如此。我在这里做错了什么?
更新
因为有人问:this.ccService.getStatus()是一个返回的接口:
export interface NgcCookieConsentStatus {
allow?: 'allow';
deny?: 'deny';
dismiss?: 'dismiss';
}
【问题讨论】:
-
@Sajeetharan 添加了一个更新来解释您的评论。
-
组件中的
hasConsented是做什么用的? -
@ConnorsFan 正如我的问题中所述,我在 ngIf 中使用它来显示/隐藏内容。
标签: angular typescript