與這個問題有關。我決定用 Promises 重寫我的代碼,但是我如何在不需要所有 Array 屬性的情況下承諾一個介面陣列?
import { Component, OnInit } from '@angular/core';
import PouchDB from 'node_modules/pouchdb';
interface Hotel {
name: string;
}
@Component({
selector: 'app-tab1',
templateUrl: 'tab1.page.html',
styleUrls: ['tab1.page.scss']
})
export class Tab1Page {
hotels: any;
item: Hotel[];
constructor() {
this.hotels = new PouchDB(<!-- db link -->, {
auto_compaction: true
});
}
ngOnInit() { this.item = this.ionViewWillEnter(); }
ionViewWillEnter(): Promise<Hotel[]> {
var promise = this.hotels.allDocs({
include_docs: true
}).then(function(result): Hotel[] {
var data = result.rows.map(function(row): Hotel {
return({name: row.doc.name});
});
return(data);
}).catch(function(err) {
console.log(err);
});
return(promise);
}
}
Type 'Promise<Hotel[]>' is missing the following properties from type 'Hotel[]': length, pop, push, concat, and 26 more.
[ng] 23 ngOnInit() { this.item = this.ionViewWillEnter(); }
uj5u.com熱心網友回復:
ionViewWillEnter() 回傳一個承諾,而不是一個值。所以你必須啟動promise 并使用then() 或await 給它一個回呼。
ngOnInit() { this.ionViewWillEnter().then(item => this.item = item); }
我建議你閱讀一些關于 promise 的檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/402121.html
