英文:
Angular 14 complains that a property on my observable object does not exist
问题
所以我已经声明:
public userEmail?: Observable<LoggedInUser | null>;
然后在我的 ngOnInit() 中:
this.Authstate.dispatch(unencryptEmail({ email: this.userKey }));
接着在我的 ngAfterContentInit() 中:
this.userEmail = this.Authstate.select(selectUserInfo);
前提是用户忘记了他们的密码,一个带有链接的电子邮件被发送,链接中包含了他们的电子邮件加密信息。所以当他们点击链接时,我首先从查询参数中获取加密信息,然后在 ngOnInit() 中解密它。然后我尝试从我的状态对象中获取电子邮件以在页面上显示它。
我的页面上有:
<div *ngIf="userEmail !== null">
{{userEmail.LoggedInUser.email | async}}
</div>
错误提示说 LoggedInUser 在 Obervable 类型 LoggedInUser 上不存在:
export interface LoggedInUser {
LoggedInUser: User,
token?: string
}
我的 User 对象有 email 属性。
解密都是正常的,问题在于显示传回的值。
这是我的 effect:
unencryptEmail$ = createEffect(
() =>
this.actions$.pipe(
ofType(fromAuthActions.unencryptEmail),
mergeMap((auth) => {
return this.authService.doUnencryptEmail(auth.email)
.pipe(map((data) => { return fromAuthActions.unencryptEmailComplete({ profile: data }); }))
})
),
{ dispatch: true }
);
和我的选择器:
export const selectUserInfo = createSelector(
getAuthFeatureState,
(state: AuthState) => state.profile
);
**** 更新 ****
根据 @Wandrille 的回答,我更改了 HTML 部分:
<div *ngIf="(userEmail | async) !== null">
你好
{{(userEmail | async)?.LoggedInUser?.email}}
</div>
现在只有当 'if' 满足时才显示你好,但我的电子邮件没有显示。使用 redux devtools,我可以看到电子邮件已经填充。
英文:
So I have declared:
public userEmail?: Observable<LoggedInUser | null>;
then in my ngOnInit() I have:
this.Authstate.dispatch(unencryptEmail({ email: this.userKey }));
then in my ngAfterContentInit() I have:
this.userEmail = this.Authstate.select(selectUserInfo);
The premise is that a user has forgotten their password, an email gets sent with a link that includes their e-mail encrypted. So when they hit the link I first get the encryption from query params and unencrypt it in ngOnInit(). I then attempt to get the email from my state object to display it on the page.
my page has:
<div *ngIf="userEmail !== null">
{{userEmail.LoggedInUser.email | async}}
</div>
The error states that LoggedInUser does not exist on Obervable type LoggedInUser:
export interface LoggedInUser {
LoggedInUser: User,
token?: string
}
my User object has the email property.
The unencrypt all works, its displaying the value passed back that is the issue.
Here is my effect:
unencryptEmail$ = createEffect(
() =>
this.actions$.pipe(
ofType(fromAuthActions.unencryptEmail),
mergeMap((auth) => {
return this.authService.doUnencryptEmail(auth.email)
.pipe(map((data) => { return fromAuthActions.unencryptEmailComplete({ profile: data }); }))
})
),
{ dispatch: true }
);
and my selector:
export const selectUserInfo = createSelector(
getAuthFeatureState,
(state: AuthState) => state.profile
);
**** Update ******
Following @Wandrille answer I changed the html bit to be:
<div *ngIf="(userEmail | async) !== null">
hello
{{(userEmail | async)?.LoggedInUser?.email}}
</div>
hello now displays only when the 'if' is met, but my email is not displayed. Using the redux devtools I can see that the email is populated.
答案1
得分: 1
"由于userEmail是一个Observable,您需要订阅它以获取其值:
例如:
<div *ngIf="(userEmail | async) !== null">
{{ (userEmail | async)?.LoggedInUser.email }}
</div>
```"
<details>
<summary>英文:</summary>
As userEmail is an Observable, you need to subscribe to it in order to get the value:
For example:
<div *ngIf="(userEmail | async) !== null">
{{ (userEmail | async)?.LoggedInUser.email }}
</div>
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论