首页 > 解决方案 > 类型对象上不存在属性

问题描述

代码:

import { Component, OnInit } from '@angular/core';
import {DataService} from '../data.service';
import {Observable} from 'rxjs';
import {ActivatedRoute} from '@angular/router';

@Component({
  selector: 'app-details',
  templateUrl: './details.component.html',
  styleUrls: ['./details.component.scss']
})
export class DetailsComponent implements OnInit {
user$: Object;
  constructor(private data: DataService, private route: ActivatedRoute) {
    this.route.params.subscribe(
      params => this.user$ = params.id
    );
  }

  ngOnInit() {
  this.data.getUser(this.user$).subscribe(
  (data: Object) =>  this.user$ = data
);
  }

}

问题是当我尝试构建时它给出了这个错误:ng build --prod

 Property 'name' does not exist on type 'Object'

我也尝试Objectany. 它没有成功。我是 Angular 6 的新手。

标签: angular

解决方案


可能您name在模板中使用user$变量绑定,因为 name$ 是您与异步数据的绑定。

因此,在这种情况下,最好的方法是——

user$: any;

并在模板中尝试像这样使用安全导航运算符(? -

{{name$?.name}}

推荐阅读