根据源代码,如果您传入任何按钮,那么您将对按钮和处理程序负全部责任。
这是来自generatePickerOptions() 方法的sn-p:
// If the user has not passed in picker buttons,
// add a cancel and ok button to the picker
const buttons = pickerOptions.buttons;
if (!buttons || buttons.length === 0) {
pickerOptions.buttons = [
{
text: this.cancelText,
role: 'cancel',
handler: () => {
this.updateDatetimeValue(this.value);
this.ionCancel.emit();
}
},
{
text: this.doneText,
handler: (data: any) => {
this.updateDatetimeValue(data);
/**
* Prevent convertDataToISO from doing any
* kind of transformation based on timezone
* This cancels out any change it attempts to make
*
* Important: Take the timezone offset based on
* the date that is currently selected, otherwise
* there can be 1 hr difference when dealing w/ DST
*/
const date = new Date(convertDataToISO(this.datetimeValue));
this.datetimeValue.tzOffset = date.getTimezoneOffset() * -1;
this.value = convertDataToISO(this.datetimeValue);
}
}
];
}
在我看来,如果你想提供自己的按钮,它会变得相当复杂,因为你不能只使用上面的这个 sn-p,它包含对私有方法的调用。
根据我正在阅读的内容,我很想在 Ionic 上打开一个错误,说该功能基本上无法使用。
除非我缺少一些理论?我不知道您是否可以编写一个处理程序,该处理程序就像它在类中的插槽一样工作,并且能够访问this 并获取内部方法。
实验
也许尝试将它们全部放在一起,看看它是否有效:
customPickerOptionFrom = {
buttons: [{
text: 'Clear',
handler: () =>
this.expirationDate = null
},
{
text: this.cancelText,
role: 'cancel',
handler: () => {
this.updateDatetimeValue(this.value);
this.ionCancel.emit();
}
},
{
text: this.doneText,
handler: (data: any) => {
this.updateDatetimeValue(data);
/**
* Prevent convertDataToISO from doing any
* kind of transformation based on timezone
* This cancels out any change it attempts to make
*
* Important: Take the timezone offset based on
* the date that is currently selected, otherwise
* there can be 1 hr difference when dealing w/ DST
*/
const date = new Date(convertDataToISO(this.datetimeValue));
this.datetimeValue.tzOffset = date.getTimezoneOffset() * -1;
this.value = convertDataToISO(this.datetimeValue);
}
}
]
}