我有一组全部使用Toastr的脚本。他们看起来像这样:
import $ from 'jquery';
import toastr from 'toastr';
import list from './data/address';
import { selectedApp, validate, getCookie, setCookie } from './common';
$(document).ready(function () {
toastr.options = {
'closeButton': true,
'debug': true,
'newestOnTop': false,
'progressBar': true,
'positionClass': 'toast-top-full-width',
'preventDuplicates': false,
'onclick': null,
'showDuration': '6000',
'hideDuration': '1000',
'timeOut': '50000',
'extendedTimeOut': '1000',
'showEasing': 'swing',
'hideEasing': 'linear',
'showMethod': 'fadeIn',
'hideMethod': 'fadeOut'
};
// --- Application code --- //
});
我有一些脚本,并且所有脚本的顶部都声明了toastr.options
。有没有一种方法(也许使用导入)可以让我全局设置 toastr 选项?
答案 0 :(得分:2)
如果要使用es6模块,则可以创建单独的toastr配置。 例如,
import toastr from "toastr";
toastr.options = {
closeButton: true,
debug: true,
newestOnTop: false,
progressBar: true,
positionClass: "toast-top-full-width",
preventDuplicates: false,
onclick: null,
showDuration: "6000",
hideDuration: "1000",
timeOut: "50000",
extendedTimeOut: "1000",
showEasing: "swing",
hideEasing: "linear",
showMethod: "fadeIn",
hideMethod: "fadeOut"
};
export default toastr;
然后只需在顶部而不是toastr
导入此文件,因为这将配置toastr
并导出已配置的toastr
。为
import toastr from './PathToAboveFile';
或者如果您想使用es5样式的全局配置,
window.toastrOptions = {...}
在单独的js文件中,并在每个html页面中引用。然后,您可以在$(document).ready
函数中进行设置。
toastr.options = window.toastrOptions;
由于Toastr选项现在位于单独的文件中,因此可以对其进行集中配置。