[英]auth0 get profile information backside
我正在尝试从 Auth0 获取用户个人资料信息。 它与 pipe 异步在前端工作
<pre *ngIf="auth.userProfile$ | async as profile">
<code>{{ profile | json }}</code>
</pre>
但是在后端如果我想读取昵称并用它做点什么。 我迷路了。
constructor(public auth: AuthService)
ngOnInit() {
if (this.auth.isAuthenticated$) {
const result = this.auth.userProfile$;
}
}
我知道我的变量“结果”是一个 Observable。 但我对 Observable 的这些东西很陌生。 我试图获得价值。 如果我使用调试控制台,我可以看到这一行的值:
this.auth.userProfile$.source.value.nickname
但是如果我在我的代码中写它,我有这个错误: Typescript 错误:属性'值'不存在于类型'Observable'
ngOnInit() {
if (this.auth.isAuthenticated$) {
const result = this.auth.userProfile$;
console.log(this.auth.userProfile$.source.value.nickname); // error here
}
}
所以有人可以帮我解决这个问题吗? 谢谢
您可以通过订阅来访问observable
中的数据(这就是async
pipe 在后台所做的事情)。
以下是如何订阅您的 observable 的示例:
// import { tap } from 'rxjs/operators';
// tap is one of many rxjs operators
// operators allow you to perform operations on data within observables
this.auth.userProfile$.pipe(
tap(profile => {
console.log(profile);
})
)
.subscribe(); // this is what allows you access the data within the observable
我终于找到了解决方案:
在 class 我输入以下代码:
export class getInfo implements OnInit {
private login_info: any;
ngOnInit() {
this.auth.getUser$().subscribe(val => {
this.login_info = val;
console.log('print info', val.nickname);
});
}
}
public useInfo(status: string) {
console.log('print info', this.login_info.nickname);
}
所以在 useInfo 方法中,我可以使用从用户配置文件中获取的信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.