Ionic FrameworkでPOCをいくつか実行していて、ActionSheetとActionSheetControllerを使用する必要がありました。すべてがうまくいくようですが、クラスで定義した関数をハンドラに割り当てると、奇妙なエラーが発生しました。奇妙なのは、コンソールログを置いたときに関数を呼び出すことができるということですが、私が注入したクラス変数やサービスがその関数で使用または参照されている場合はエラーとなります。私のサービスやクラス変数は未定義です。以下のコードを参照してください:ActionSheetハンドラーがクラス変数にアクセスできない
addToFavorites() {
console.log('Adding to Favorites', this.game.id);
this.favorite = this.favoriteservice.addFavorite(this.game.id);
this.toastCtrl.create({
message: this.game.id + ' added as favorite successfully',
position: 'middle',
duration: 3000}).present();
}
presentActionSheet() {
let actionSheet = this.actionSheetCtrl.create({
title: 'Select Actions',
buttons: [
{
text: 'Add To Favorites',
handler: this.addToFavorites
},
{
text: 'Cancel',
role: 'cancel',
handler:() => {
console.log('Cancel clicked');
}
}
]
});
actionSheet.present();
}
どちらも私が挿入したfavoriteServiceのページクラス内で定義され、ゲームはクラス変数です。私は、私は以下のようなaddToFavoritesを呼び出すとき、それは完璧に動作することが見つかりました:1つの作品やその他がなかった理由を
presentActionSheet() {
let actionSheet = this.actionSheetCtrl.create({
title: 'Select Actions',
buttons: [
{
text: 'Add To Favorites',
handler:() => {
this.addToFavorites();
}
},
{
text: 'Cancel',
role: 'cancel',
handler:() => {
console.log('Cancel clicked');
}
}
]
});
actionSheet.present();
}
することは、誰かが私に説明できますか?違いは何ですか?
ありがとうございました。
EDIT:これは私は、構文@ewizardが提案使うときに来るのエラーです:
[ts]
Argument of type '{ title: string; buttons: ({ text: string; handler: void; } | { text: string; handler:() => void...' is not assignable to parameter of type 'ActionSheetOptions'.
Types of property 'buttons' are incompatible.
Type '({ text: string; handler: void; } | { text: string; handler:() => void; } | { text: string; role...' is not assignable to type '(string | ActionSheetButton)[]'.
Type '{ text: string; handler: void; } | { text: string; handler:() => void; } | { text: string; role:...' is not assignable to type 'string | ActionSheetButton'.
Type '{ text: string; handler: void; }' is not assignable to type 'string | ActionSheetButton'.
Type '{ text: string; handler: void; }' is not assignable to type 'ActionSheetButton'.
Types of property 'handler' are incompatible.
Type 'void' is not assignable to type '() => boolean | void'.
私の答えが役に立ったら、それを正しく、ありがとうとマークしてください。 – ewizard
あなたが同じシステムに戻ってきたときに私に知らせて、エラーを読むことができます。 – ewizard