javascript - 使用 javascript 全局设置 toastr 选项

标签 javascript toastr

我有一组全部使用 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 选项?

最佳答案

如果你想使用 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 选项位于单独的文件中,因此可以集中配置它。

关于javascript - 使用 javascript 全局设置 toastr 选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57867657/

相关文章:

javascript - 如何构建 Javascript 数组 Delta

javascript - 根据消息类型设置 toastr 选项 (Aurelia)

javascript - 如何在 toastr 中显示确认对话框

使用 requirejs 未定义的 toastr

javascript - 错误: Cannot find module 'dbconstant'

javascript - Adobe Acrobat Javascript promise

javascript - WooCommerce 图像缩放

javascript - javascript 中对象内的对象数组

angularjs - 在 AngularJS 应用程序中安装 toastr

typescript - toastr.js 如何在 Aurelia 和 Typescript 中工作?