“对象"不包含此类成员Angular 5 [英] 'object' does not contain such a member Angular 5

查看:147
本文介绍了“对象"不包含此类成员Angular 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Angular的新手,正在尝试制作一个小型应用程序.

I am new to Angular and trying to make a small application.

我提到"object"没有包含这样的成员 回答,但我没有从那里得到解决方案.

I referred 'object' does not contain such a member answer but I am not getting my solution from there.

profile.component.ts

import { Component, OnInit } from '@angular/core';
import { AuthService } from '../../services/auth.service';
import { Router } from '@angular/router';

@Component({
  selector: 'app-profile',
  templateUrl: './profile.component.html',
  styleUrls: ['./profile.component.css']
})
export class ProfileComponent implements OnInit {
  user: Object;
  constructor(private authService: AuthService, private roter: Router) {}

  ngOnInit() {
    this.authService.getProfile().subscribe(
      profile => {
        this.user = profile.user;
      },
      err => {
        console.log(err);
        return false;
      }
    );
  }
}

profile.component.html

<div *ngIf="user">
  <h2 class="page-header">
    {{ user.name }}
  </h2>
  <ul class="list-group">
    <li class="list-group-item">
      Username: {{ user.username }}
    </li>
    <li class="list-group-item">
      Email: {{ user.email }}
    </li>
  </ul>
</div>

Visual Studio代码正在显示此内容 错误:

Visual studio code is showing this Error:

[角度]标识符用户名"未定义. 对象"不包含此类成员

[Angular] Identifier 'username' is not defined. 'Object' does not contain such a member

ProfileComponent的财产用户

property user of ProfileComponent

推荐答案

可以更改

user: Object; 

通过

user: any;

这在您profile.component.ts中肯定会起作用,因为最初您已将其声明为对象,所以在运行或构建应用程序打字稿时编译失败,原因是作为user.username进行访问. 您将类型更改为任何,或者创建具有必需属性的界面或类型,然后将此类型分配给用户 前任: profile.component.ts:

In your profile.component.ts this will surely work,because initially you have declared it as object so while running the or building app typescript compilation fails due to accessed as user.username. Either you change the type to any or create interface or type having required properties and assign this type to user Ex: profile.component.ts:

interface userObject {
  username:string,
  password:string
} 

export class ProfileComponent implements OnInit {
   user : userObject;
}

这篇关于“对象"不包含此类成员Angular 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆