我一直試圖弄清楚這一點,但沒有任何成功。將不勝感激一些幫助!我正在使用 Angular 版本 13,并嘗試執行獲取請求以從資料庫中獲取物件陣列。我收到錯誤訊息:“訂閱”型別缺少“物件 []”型別的以下屬性:長度、彈出、推送、連接和 28 個以上。
app.components.ts
import { Component, OnInit } from '@angular/core';
import { SEOService } from './services/shared/seo.service';
import { StartupService } from './services/shared/startup.service';
import { Object } from './services/shared/object';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit{
siteUrl: string;
pageObjects: Object[];
pageConfig: any;
constructor(private startupService: StartupService, private seoService: SEOService){
this.siteUrl = '';
this.pageObjects = [];
this.pageConfig = {};
};
ngOnInit(){
this.siteUrl = window.location.href;
this.pageConfig = this.startupService.getPageConfig(this.siteUrl);
this.seoService.updateTitle(this.pageConfig.title);
this.seoService.updateDescription(this.pageConfig.description);
this.pageObjects = this.startupService.getPageObjects(1).subscribe(
(response: Object[]) => {
this.pageObjects = response;
},
(error) => {
console.log(error);
}
);
};
};
啟動服務.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { Object } from './object';
@Injectable()
export class StartupService {
constructor(private httpClient: HttpClient){};
getPageConfig(requestUrl: string): any[]{
var pageConfig: any = {
type: 'webproject',
title: 'Yellow Pages & Classifieds',
description: 'Yellow Pages & Classifieds'
};
return pageConfig;
};
getPageObjects(pageNID: number): Observable<Object[]>{
return this.httpClient.get<Object[]>('http://localhost:1040/objects/getallbypage/' pageNID);
}
};
物件.ts
export class Object {
_id: string;
nid: number;
id: string;
title: string;
type: string;
path: string;
ofnid: number;
of: string;
loadpriority: number;
isactive: boolean;
pagenid: number;
constructor(){
this._id = '';
this.nid = 0;
this.id = '';
this.title = '';
this.type = '';
this.path = '';
this.ofnid = 0;
this.of = '';
this.loadpriority = 0;
this.isactive = true;
this.pagenid = 0;
};
};
uj5u.com熱心網友回復:
只是一個愚蠢的錯誤,你不需要這個額外的任務:
this.pageObjects = this.startupService.getPageObjects(1).subscribe(
...
)
改成
this.startupService.getPageObjects(1).subscribe(
...
)
值得一提的是,它的回傳值Observable.subscribe()是 type Subscription。堅持它可以讓您取消訂閱,這對于永不停止發出值的訂閱是必要的。使用該服務發出簡單的 HTTP 請求時,您無需擔心它,HTTPClient因為它們只發出一個值,然后自行清理。
uj5u.com熱心網友回復:
您將pageObjects屬性分配給實際的 observable,然后將相同的屬性重新分配給該 observable 的回應。
您對訂閱的結果感興趣,因此只需擺脫分配pageObjects給可觀察物件的部分。
this.startupService.getPageObjects(1).subscribe(
(response: Object[]) => {
this.pageObjects = response;
},
(error) => {
console.log(error);
}
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/490457.html
標籤:有角度的
