是否可以为每个toast类型单独设置toastr选项?
我的toastr代码
toastr.options = {
"closeButton": true,
"debug": false,
"newestOnTop": false,
"progressBar": false,
"positionClass": "toast-bottom-right",
"preventDuplicates": false,
"onclick": null,
"showDuration": "300",
"hideDuration": "1000",
"timeOut": "60000",
"extendedTimeOut": "60000",
"showEasing": "swing",
"hideEasing": "linear",
"showMethod": "fadeIn",
"hideMethod": "fadeOut"
};
const infoMessage = $(".js-toast-message-info").html();
if (infoMessage && infoMessage.length > 0) {
toastr.info(infoMessage);
}
const errorMessage = $(".js-toast-message-error").html();
if (errorMessage && errorMessage.length > 0) {
toastr.error(errorMessage);
}
const warningMessage = $(".js-toast-message-warning").html();
if (warningMessage && warningMessage.length > 0) {
toastr.warning(warningMessage);
}
const successMessage = $(".js-toast-message-success").html();
if (successMessage && successMessage.length > 0) {
toastr.success(successMessage);
}
我已经尝试将我想要的不同选项类型放在特定toastr类型的if语句中,但没有结果。
有什么简单的方法可以做到这一点吗?
发布于 2017-08-25 17:26:08
您可以创建类,例如Notifier,它允许您为特定消息类型定义不同的设置。
这里有一个简单的解决方案:
toastr.options = {
"closeButton": true,
"debug": false,
"newestOnTop": false,
"progressBar": false,
"positionClass": "toast-bottom-right",
"preventDuplicates": false,
"onclick": null,
"showDuration": "300",
"hideDuration": "1000",
"timeOut": "60000",
"extendedTimeOut": "60000",
"showEasing": "swing",
"hideEasing": "linear",
"showMethod": "fadeIn",
"hideMethod": "fadeOut"
};
class Notifier {
constructor(opt) {
this.dflt = {
info: {
"closeButton": false
},
success: {
"progressBar": true
},
warning: {
},
error: {
}
}
this.cfg = _.defaults(opt, this.dflt);
}
info(msg, tl, cfgOvr) {
this.notify('info', msg, tl, cfgOvr);
}
success(msg, tl, cfgOvr) {
this.notify('success', msg, tl, cfgOvr);
}
warning(msg, tl, cfgOvr) {
this.notify('warning', msg, tl, cfgOvr);
}
error(msg, tl, cfgOvr) {
this.notify('error', msg, tl, cfgOvr);
}
notify(lvl, msg, tl, cfgOvr) {
let cfg = this.cfg[lvl];
if (cfgOvr) {
cfg = _.defaults(cfgOvr, cfg);
}
toastr[lvl](msg, tl, cfg);
}
}
const notifier = new Notifier();
notifier.info('a', 'b');
上面的优点是,您可以设置默认值,在构造函数中覆盖它们,还可以覆盖特定的消息用法。
发布于 2021-07-03 10:22:37
您可以将配置选项作为第三个参数传递,如下所示
toastr.warning('Please Allow Pop-ups for multiple selection','Popup Blocked',{
"timeOut": 0,
"extendedTimeOut": 0,
"preventDuplicates": true,
"disableTimeOut" : true,
});
这不会覆盖您的全局配置
https://stackoverflow.com/questions/45877521
复制相似问题