私はAngularを初めて使用し、小さなアプリケーションを作成しようとしています。
私が参照した「オブジェクト」にはそのようなメンバーの回答が含まれていませんが、そこから解決策が得られません。
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はこのエラーを示しています:
[角度]識別子 'username'が定義されていません。「オブジェクト」にはそのようなメンバーが含まれていません
ProfileComponentのプロパティユーザー
どちらかを変更します
user: Object;
沿って
user: any;
profile.component.tsでは、これは確実に機能します。これは、最初にオブジェクトとして宣言しているため、実行中またはビルド中のアプリのtypescriptコンパイルが、user.usernameとしてアクセスされたために失敗するためです。タイプをanyに変更するか、必要なプロパティを持つインターフェイスまたはタイプを作成して、このタイプをユーザーに割り当てます。例:profile.component.ts:
interface userObject {
username:string,
password:string
}
としてアクセス
export class ProfileComponent implements OnInit {
user : userObject;
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加