【发布时间】:2021-01-10 20:44:49
【问题描述】:
所以我打算让 NG 在组件的生命周期内处理订阅,让它自动订阅/取消订阅。
这是我的服务,为外部端点的调用提供 observable,MySvc.ts
import { Injectable } from '@angular/core';
import { HttpClient, HttpRequest, HttpHeaders, HttpErrorResponse } from "@angular/common/http";
import { Observable, throwError } from 'rxjs';
const url = '...';
@Injectable()
export class MySvc{
constructor(private http: HttpClient) { }
getAllRec()
{
return this.http.get(url).pipe(map((res: Response) => res.json()));
}
myRec.ts
export interface myRec
{
name: string;
}
我的展示组件MyPage.ts
import { Component, OnInit } from '@angular/core';
import { MySvc } from '..MySvc.service';
import { Observable, of} from "rxjs";
import { myRec} from '../models/myRec';
@Component({...})
export class MyPage implements OnInit {
public allRec$: Observable<myRec[]>;
constructor(private MySvc: MySvc) {}
ngOnInit()
{
this.allRec$ = this.MySvc.getAllRec(); // see compiling error below
}
类型 'Observable
' 不可分配给类型 '可观察的 '。类型“承诺”缺少以下内容 'myRec[]' 类型的属性:length、pop、push、concat 等 26 个
【问题讨论】:
标签: angular