我正在使用print This来打印应用程序中页面中的一堆元素.
我遇到以下错误:
leaflet.js:5 Uncaught Error: Map container is already initialized.
at e._initContainer (leaflet.js:5:31752)
at e.initialize (leaflet.js:5:20608)
at new e (leaflet.js:5:2745)
at o.map (leaflet.js:6:8323)
at initLeafletMap (JsCombinerV22023702496814.ashx?jsLocal=true:57688:22)
at StatisticMapControl.LoadLeafletMap (JsCombinerV22023702496814.ashx?jsLocal=true:57375:24)
at eval (eval at <anonymous> (jquery.min.js:2:14136), <anonymous>:2:36)
at eval (<anonymous>)
at jquery.min.js:2:14136
at Function.globalEval (jquery.min.js:2:14147)
但这只有在我加载应用程序页面时第一次单击"打印"时才会发生.如果加载后,我导航不同的菜单并try 在这些菜单中打印,则不会出现任何错误.如果我回到默认页面,也不会出现任何错误.
错误发生在传单文件中(来自图书馆)
_initContainer: function(t) {
var e = this._container = o.DomUtil.get(t);
if (!e)
throw new Error("Map container not found.");
if (e._leaflet_id)
throw new Error("Map container is already initialized.");
o.DomEvent.addListener(e, "scroll", this._onScroll, this),
this._containerId = o.Util.stamp(e)
},
当我调用它时:
function initLeafletMap(mapContainerId) {
return L.map(mapContainerId, { zoomControl: false, tap: false, zoomSnap: 0 });
};
调用后,传单内部运行的部分代码如下:
o.Class.extend = function(t) {
var e = function() {
this.initialize && this.initialize.apply(this, arguments),
this.callInitHooks()
}
initialize似乎被调用了两次,然后又调用initContainer,其中出现 map 容器已经初始化的异常.
我是否应该修改该函数中的某些内容,以便只调用一次初始化?我不想修改外部库.
我研究过,在很多地方,他们建议在调用该函数之前将 map 设置为空.在我的例子中,这已经在调用 map 之前完成了:
this.map = null;
this.map = initLeafletMap(mapContainerId);
在使用print This之前,使用了其他东西(我不确定是什么)来从页面中抓取所需的元素并形成文档以进行打印.Leaflet.js也被使用,没有造成任何问题.
如果有人知道问题可能是什么或者我可以try 什么,我将不胜感激.
UPDATE
正如Jason的请求,这就是我调用print This的方式:
namespace('WebPageExportationService').getInstance = function () {
function webPageExportationService() {
this.callback = null;
this.exportPage = function (exportRequest, callback) {
this.callback = callback;
var cssContent = exportRequest.customProperties.cssContent;
var elementsToPrint = exportRequest.customProperties.elementsToPrint;
var header = exportRequest.customProperties.customHeader;
var elementsJoined = elementsToPrint.join(',');
$(elementsJoined).printThis({
debug: true,
printDelay: 2000,
importStyle: true,
loadCSS: cssContent,
header: header,
afterPrint: function() {
if (typeof callback === 'function') {
callback();
}
}
});
};
}
return new webPageExportationService();
}
它被放置在前端的一个文件中,我这样称呼它:
this.ExportRedirect = function () {
var dashboardTitle = $('#PaintMetaDataControl1 ul li.metadataItem').last().text();
var cssFileUrl = window.location.origin + '/CSS/printThisStyle.css';
var header = "<img src='" + window.location.origin + "/Img/header.png' />";
var request = {
downloadedFileName: dashboardTitle,
customProperties: null
};
var elements = [];
elements = ["#CongestionDetail", ".wrap-controller", "#Comp1", "#Comp2", "#CompTasa3", "#statistic-table", "#Type .StatisticTableControl", "#tipoRes", "#group5anios", "#Recurr1", "#Recurr2", ".prefooter"];
var printProperties = {
cssContent: cssFileUrl,
elementsToPrint: elements,
customHeader: header
}
request.customProperties = Object.assign({}, request.customProperties, printProperties);
//window.waiting.Show();
jQuery(document).off().on('iFramePrintLoaded', function () {
window.waiting.Hide();
});
namespace('WebPageExportationService').getInstance().exportPage(request, function () {
window.waiting.Hide();
});
LoadingSpinnerChangingDashboards();
};
我判断了使用设置dev:true创建的iframe.我需要的所有元素都在iframe中.它还包括并利用Leaflet库,与我检索要打印的元素的页面相同.这是因为我正在使用库打印交互式 map
<link type="text/css" rel="stylesheet" href="https://unpkg.com/leaflet@1.0.1/dist/leaflet.css" media="all">
我不确定如何使用beforePrint事件选项.我应该删除iframe中对Leaflet的调用吗?我认为这不允许我从网页中 Select map .