angular - 角度可观察的不起作用
问题描述
我正在尝试将 Firebase 电话身份验证添加到我的应用程序中。我正需要创建一个 reCAPTCHA 对象。我想知道 reCAPTCHA 是否已解决。
在模板中:
{{ recaptchaSolved | async | json }}
这是我的组件类:
export class AddComponent implements OnInit, OnDestroy {
windowRef: any;
@ViewChild('recaptchaContainer') recaptchaContainer: ElementRef;
recaptchaSolved: Observable<boolean>;
constructor(
private win: WindowService
) {}
ngOnInit() {
this.windowRef = this.win.windowRef;
firebase.auth().useDeviceLanguage();
this.windowRef.recaptchaVerifier = new firebase.auth.RecaptchaVerifier(
this.recaptchaContainer.nativeElement, // for visible recaptcha
{
size: 'small',
callback: response => {
console.log(`reCAPTCHA solved`);
this.recaptchaSolved = of(true);
},
'expired-callback': () => {
console.log(`reCAPTCHA expired`);
this.recaptchaSolved = of(false);
}
}
);
}
ngOnDestroy() {}
}
窗口服务:
@Injectable()
export class WindowService {
get windowRef() {
return window;
}
}
问题是当 reCAPTCHA 被解决时,我在控制台中记录了“reCAPTCHA 已解决”,但 observable 没有更新(它为空)。
解决方案
使用源Subject
并观察它。
export class AddComponent implements OnInit, OnDestroy {
recaptchaSolvedSource = new Subject<boolean>();
recaptchaSolved = this.recaptchaSolved.asObservable();
constructor(
private win: WindowService
) {}
ngOnInit() {
...
this.windowRef.recaptchaVerifier = new firebase.auth.RecaptchaVerifier(
this.recaptchaContainer.nativeElement, // for visible recaptcha
{
size: 'small',
callback: response => {
this.recaptchaSolvedSource.next(true);
},
'expired-callback': () => {
this.recaptchaSolvedSource.next(false);
}
}
);
}
ngOnDestroy() {}
}
推荐阅读
- database - 我可以以某种方式提供额外的上下文以允许 postgres 有效地排序/限制视图而不计算它们的行吗?
- java - 创建自定义参数匹配器混淆实现
- testflight - 上传到 TestFlight 时,Fastlane 获得“train_version 不再是 FastlaneCore 消息中使用的参数”
- javascript - 如何将道具从 connect 的 HOC 传递到 HOC?
- google-bigquery - 查询失败并出现错误 RESOURCE_EXHAUSTED (ORDER BY without ORDER BY)
- docker - Ubuntu18.04下默认无法安装nvidia-docker2
- typescript3.0 - TypeScript const 断言:如何使用 Array.prototype.includes?
- angular - Angular 6 kendo-dropdownlist 有条件地设置 [valueField] 和 [textField]
- dart - Google Rest API:“无效的 JWT:观众检查失败”?我正在使用观众字段 API 建议
- typescript - 如何将 Styled API 与 typescript 和 props 一起使用