'object' 不包含这样的成员 Angular 5 [英] 'object' does not contain such a member Angular 5

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

问题描述

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

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

profile.component.ts

import { Component, OnInit } from '@angular/core';从 '../../services/auth.service' 导入 { AuthService };从@angular/router"导入{路由器};@成分({选择器:应用程序配置文件",templateUrl: './profile.component.html',styleUrls: ['./profile.component.css']})导出类 ProfileComponent 实现 OnInit {用户:对象;构造函数(私有 authService:AuthService,私有转子:路由器){}ngOnInit() {this.authService.getProfile().subscribe(个人资料 =>{this.user = profile.user;},错误 =>{控制台日志(错误);返回假;});}}

profile.component.html

<h2 class="page-header">{{ 用户名 }}<ul class="list-group"><li class="list-group-item">用户名:{{ user.username }}<li class="list-group-item">电子邮件:{{ user.email }}

Visual Studio 代码显示了这一点错误:

<块引用>

[Angular] 标识符用户名"未定义.对象"不包含这样的成员

ProfileComponent 的属性用户

解决方案

要么改变

用户:对象;

用户:任意;

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

interface userObject {用户名:字符串,密码:字符串}

访问权限

导出类 ProfileComponent 实现 OnInit {用户:用户对象;}

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

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 code is showing this Error:

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

property user of ProfileComponent

解决方案

Either change

user: Object; 

by

user: any;

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
} 

access as

export class ProfileComponent implements OnInit {
   user : userObject;
}

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

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