更新私はreturn
コンソールログの後に置いて、問題が約束ではないと判断しました-それはそれをうまく読んでいます。以下のコードで強調表示している行this.contentItems = cpl.ary as ContentItem[];
で、エラーが発生している場所です。これが、4つのアイテムの配列であるcpl
1つのプロパティを持つオブジェクトであることを確認しましたary
。コンソールログに正常に表示されます。しかし、私がary
プロパティを取得しようとすると、as ContentItem[]
それがnull
思い付きContent[]
、左側の私の空に割り当てようとします。したがって、これは純粋にキャストまたはその他のタイプ割り当ての問題です。答えが見つかったら、この質問の件名を変更します。
私の問題は、content-display.component.tsのメソッドでthis.contentItems
nullに設定されることthen
です。
私が消費しているJSONは次のとおりです。
{ "ary":[
{
"id": "1",
"name": "Test Project",
"nextDate": "2016-12-01T16:00:00",
"dateDescription": "ends",
"contentType": "project",
"link": "http://google.com",
"topics": ["1"]
},
{
"id": "3",
"name": "Dummy Article",
"nextDate": "2016-12-21T00:00:00",
"dateDescription": "expires",
"contentType": "article",
"link": "http://msn.com",
"topics": ["2"]
}
]}
私のcontent-item.service.ts:
getContentItems(): Promise<ContentPayload> {
if( window.location.hostname === "localhost" )
return Promise.resolve( CONTENTITEMS );
else
return this.http.get( this.contentUrl )
.toPromise()
.then( (response) => { return response.json() } )
.then( (data) => { return data as ContentPayload })
.catch( this.handleError );
}
そして、私のコンテンツに戻ります-display.component.ts
private contentItems: ContentItem[] = [];
getContentItems(): void {
this.contentItemService.getContentItems()
.then( (contentPayload) => this.getArrayFromPayload )
.then( this.getSelectOptions );
}
getArrayFromPayload( cpl: ContentPayload ): void {
this.contentItems = cpl["ary"];
/********** above, this.contentItems is set to null ************/
this.filteredItems = this.contentItems;
}
ContentPayload:
export class ContentPayload {
ary: any[];
}
ContentItem:
export class ContentItem {
id: string;
name: string;
nextDate: Date;
dateDescription: string;
contentType: string;
link: string;
isClicked?: boolean = false;
topics: string[];
}
これは完全を期すために含まれています。それが何をするかは重要ではありません、エラーは上記で発生します。
getSelectOptions( ): void {
// return TOPICMAP;
var topicMap: Topic[] = TOPICMAP;
var filteredTopicMap: Topic[] = [];
var uniqueValues: string[] = this.makeContentSet( this.getTopicIDs( this.contentItems ) );
for( var i = 0; i < uniqueValues.length; i++ ) {
filteredTopicMap.push( this.getTopicWithID( topicMap, uniqueValues[i] ) );
}
this.selectOptions = filteredTopicMap;
}
そして、このコンポーネントの私のテンプレート(スタイルの不一致と「これ」の使用は最終的にクリーンアップされます。iPadにバックティックがないことをご存知ですか?):
template: `
<h2>{{title}}</h2>
<div>Maximum of 20 items</div>
<div>Order:
<span [class.clicked]="latestOldest==='latest'" (click)="mySort('latest')">latest first</span>
|
<span [class.clicked]="latestOldest==='oldest'" (click)="mySort('oldest');">oldest first</span>
</div>
<div>
Filters:
<ng-select
[options] = "this.selectOptions"
[multiple] = "this.showMultiple"
placeholder = "Select topics"
[allowClear] = "true"
theme = "default"
(selected) = "onSelected( $event )"
(deselected) = "onDeselected( $event )">
</ng-select>
<ul>
<li *ngFor="let contentItem of filteredItems"
(click)="onClick(contentItem)"
class="{{contentItem.contentType}}"
[class.clicked]="contentItem.isClicked">
<h3>{{contentItem.name}}</h3>
<div>{{ contentItem.contentType | uppercase }} {{ contentItem.dateDescription}}
{{ contentItem.nextDate.toGMTString() | date:'mediumDate' }}
{{ (contentItem.contentType !== "article")? (contentItem.nextDate | date:'shortTime'): '' }}
</div>
</li>
</ul>
</div>
`
あなたの問題はthis
正しく取り扱わないことが原因です。まさにこのトピックに関して、ここには多くの質問があります。
あなたが以下を書くとき
getContentItems(): void {
this.contentItemService.getContentItems()
.then( (contentPayload) => this.getArrayFromPayload )
.then( this.getSelectOptions );
}
次に、基本的に、それが定義されたオブジェクトの関数を(あなたの場合のコンポーネントインスタンスから)引き出します。関数はまだいくつかを参照してthis
いますが、this
それ以上コンポーネントで呼び出されることはありません。それが問題の原因です。
使用に関するコメントで言及されている方法.then( this.getArrayFromPayload )
も機能しません。これは、関数が呼び出されたときにコンテキストも変更するためです。
これを修正する方法はいくつかあります。このようbind()
に、then
メソッドに渡された関数を呼び出し元コンテキストのthisに渡すことができます。
.then(this.getArrayFromPayload.bind(this))
そうするthis
ことで、関数が呼び出されたときに保持されるように、現在使用されている関数が設定されます。(基本的に、この参照が正しい新しい関数を返します)
別の方法は、独自のthis
コンテキストを持たないため、外部を参照する矢印関数を使用することthis
です。
getArrayFromPayload = (cpl: ContentPayload) => {
this.contentItems = cpl["ary"];
this.filteredItems = this.contentItems;
}
そうすることで、のように呼び出すことができます.then( this.getArrayFromPayload )
。
基礎となる概念の詳細についてはthis
、JavaScriptの基本をカバーするこの素晴らしい回答を参照してください:コールバック内の正しい `this`コンテキストにアクセスする方法は?
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加