类型"{}"不可分配给类型“个人资料"

编程入门 行业动态 更新时间:2024-10-28 01:17:28
本文介绍了类型"{}"不可分配给类型“个人资料"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是打字稿新手,并且收到错误消息

I'm new to typescript and im getting the error

类型"{}"不能分配给类型个人资料".

Type '{}' is not assignable to type 'Profile'.

在 this.profile 上如何解决此问题?

import { Component } from '@angular/core'; import { IonicPage, NavController, NavParams } from 'ionic-angular'; import { AngularFireAuth } from 'angularfire2/auth'; import { AngularFirestore } from 'angularfire2/firestore'; import { Profile } from './../../models/profile'; @IonicPage() @Component({ selector: 'page-profile', templateUrl: 'profile.html', }) export class ProfilePage { profile = {} as Profile; constructor(private afs: AngularFirestore, private afAuth: AngularFireAuth, public navCtrl: NavController, public navParams: NavParams) { } ionViewWillLoad() { this.afAuth.authState.take(1).subscribe(data => { let profileCollection = this.afs.collection('profiles').doc(`${data.uid}`).valueChanges(); profileCollection.subscribe(profile => this.profile = profile); }) } }

推荐答案

您的问题是,您的 Profile 类型可能具有强制属性.您只需要使它们可选或将它们初始化为默认值即可.您不能将空对象分配给具有属性的类型.

Your problem here is that probably your Profile type has mandatory properties. You just have to either make them optional or initialize them to default values. You can’t assign an empty object to a type with properties.

您也可以只说 profile:Profile 而无需初始化它.您仍在强制使用该类型.由于您只在订阅中分配它,因此您无需进行 {} 空对象分配.

What you can do is also just say profile: Profile without initialising it. You’re still enforcing the type. Since you are only assigning it inside the subscribe you don’t need to do the {} empty object assignment.

更多推荐

类型"{}"不可分配给类型“个人资料"

本文发布于:2023-11-14 02:32:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585990.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   不可分   个人资料   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!