简体   繁体   English

类型“对象”上不存在属性“用户”。 Angular

[英]Property 'user' does not exist on type 'Object'. Angular

I'm trying to subscribe to an async function in Angular. VSCode marks subscribe as deprecated & res['user'] marks: Property 'user' does not exist on type 'Object' .我正在尝试订阅 Angular 中的异步 function。VSCode 将subscribe标记为已弃用 & res['user'] marks: Property 'user' does not exist on type 'Object'

 getUser(){ this.userService.getUserProfile().subscribe( res => { this.user = res['user']; }, err => { console.log(err); } ); }

 getUserProfile() { return this.http.get(this.url + '/userProfile'); }

subscribe itself isn't deprecated, only the variant you are using is deprecated. subscribe本身并没有被弃用,只有您正在使用的变体被弃用。 You'd rather write your code as follows:您宁愿按如下方式编写代码:

getUser(){
    this.getUserProfile().subscribe({
        next: (res) => {
            this.user = res.user;
        },
        error: (err) => {
            console.log(err);
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM