我正在尝试从服务订阅 observable,它构建时没有错误,但在浏览器中查看时出现错误“this.service.getBanners(...).subscribe is not a function”。
服务:
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
@Injectable()
export class BannerService {
banners: any = ['1','2','3'];
constructor(
) {}
getBanners(): Observable<any[]> {
return this.banners;
}
setBanners(banners: any[]): void {
this.banners = banners;
}
}
零件:
import { Component, ViewEncapsulation, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { BannerService } from './../banner/banner.service';
@Component({
selector: '.banner',
templateUrl: './banner.component.html',
styleUrls: ['./banner.component.sass'],
encapsulation: ViewEncapsulation.None
})
export class BannerComponent implements OnInit {
banners: any[];
constructor(private bannerService: BannerService){
}
ngOnInit() {
this.bannerService.getBanners().subscribe(banners => this.banners = banners);
}
}
任何想法我做错了什么?