角5-属性'subscribe'在类型'()=> Observable <any>'上不存在

时间:2018-07-13 12:59:08

标签: angular

我只是在编写一个简单的Angular实验,并且遇到以下错误... Property 'subscribe' does not exist on type '() => Observable<any>'我试图通过服务将数据中的静态json数据通过服务加载到组件中。我不确定为什么会发生这种情况,就像我之前做过很多次一样,这是我的Angular服务...

@Injectable({
    providedIn: 'root'
})
export class DashboardService {

    constructor(private http: HttpClient) {
   }

    public getDashboardData(): Observable<any> {
        return this.http.get('../../data/dashboard-data.json');
    }
}

这里没什么奇怪的...现在让我们看看我的组件

@Component({
  selector: 'app-dashboard',
  templateUrl: './dashboard.component.html',
  styleUrls: ['./dashboard.component.scss']
})
export class DashboardComponent implements OnInit {

  constructor(private dashboardService: DashboardService) { }

  ngOnInit() {
    this.dashboardService.getDashboardData.subscribe((result) => {
      console.log(result);
    });
  }
}

我看不出有什么问题吗?我已经做过很多次了。静态文件中的JSON有效且不合法。我肯定错过了一些非常简单的东西,但是我无法理解或看到我在做错什么。任何建议表示赞赏。

1 个答案:

答案 0 :(得分:2)

getDashboardData() 是不是变量的函数,您需要将其更改为

   this.dashboardService.getDashboardData().subscribe((result) => {
          console.log(result);
    });