订阅不是错误功能
我正在努力订阅 observable 从服务中,它是没有错误的建立,但我弄错了 "this.service.getBanners/.../.subscribe is not a function" 在浏览器中查看时。
服务:
成分:
有什么想法我做错了吗?
</any[]>
服务:
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/;
}
}
有什么想法我做错了吗?
</any[]>
没有找到相关结果
已邀请:
2 个回复
莫问
赞同来自:
为了 Angular <= 5.x.x
为了 Angular >= 6.x.x
</any[]></any[]>
风见雨下
赞同来自:
你宣布你的功能 getBanners// 返回可观察,但返回数组。 因此,更改您的申请以返回
您还需要添加它:
这是一个糟糕的做法,并将包括整个图书馆。 rxjs 进入您的代码:
仅导入您需要的东西。 更换更高