简体   繁体   English

使用javascript全局设置烤面包机选项

[英]set toastr options globally using javascript

I have a set of scripts that all use toastr. 我有一组全部使用Toastr的脚本。 They look a bit like this: 他们看起来像这样:

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 --- //
});

I have a few scripts and they all have the toastr.options declared at the top. 我有一些脚本,并且所有脚本都在toastr.options声明了toastr.options Is there a way (perhaps using an import) that will allow me to set the toastr options globally? 有没有一种方法(也许使用导入)可以让我全局设置烤面包机选项?

If you want to use es6 modules, then you can create a separate toastr configuration. 如果要使用es6模块,则可以创建单独的toastr配置。 For instance, 例如,

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;

Then simply importing this file at the top instead of toastr since this configures toastr and export the configured toastr . 然后只需在顶部而不是toastr导入该文件,因为这将配置toastr并导出已配置的toastr As

import toastr from './PathToAboveFile';

Or if you want an es5 style global configuration use, 或者,如果您想使用es5样式的全局配置,

window.toastrOptions = {...}

in a seperate js file and reference in each html page. 在单独的js文件中,并在每个html页面中引用。 Then in $(document).ready function you can set it. 然后在$(document).ready函数中可以进行设置。

toastr.options = window.toastrOptions;

Since now toastr options are in a separate file, it can be centrally configured. 由于现在Toastr选项位于单独的文件中,因此可以对其进行集中配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM