Angular 14抱怨我的可观察对象上不存在的属性。

huangapple go评论66阅读模式
英文:

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&lt;LoggedInUser | null&gt;;

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:

&lt;div *ngIf=&quot;userEmail !== null&quot;&gt;
    {{userEmail.LoggedInUser.email | async}}
&lt;/div&gt;

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(
    () =&gt;
      this.actions$.pipe(
        ofType(fromAuthActions.unencryptEmail),
        mergeMap((auth) =&gt; {
          return this.authService.doUnencryptEmail(auth.email)
            .pipe(map((data) =&gt; { return fromAuthActions.unencryptEmailComplete({ profile: data });  }))
        })
      ),
    { dispatch: true }
  );

and my selector:

export const selectUserInfo = createSelector(
  getAuthFeatureState,
  (state: AuthState) =&gt; state.profile
);

**** Update ******

Following @Wandrille answer I changed the html bit to be:

&lt;div *ngIf=&quot;(userEmail | async) !== null&quot;&gt;
   hello
   {{(userEmail | async)?.LoggedInUser?.email}}
&lt;/div&gt;

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>



huangapple
  • 本文由 发表于 2023年4月19日 17:22:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052820.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定