需求:
在A页面中选择一个对象,查看详情时打开一个新的浏览器网页。
再次在A页面中选择同一个对象,打开查看该对象时,要将之前打开的页面再次置前,而不是为同一个对象打开多个页面窗口。
思路:
在A页面保存所有打开的窗口对象,在打开窗口之前判断该页面是否已存在,如果已存在就选中该页面置前,如果不存在就打开一个新的窗口。
childWindow: any[] = [];
GoToDetails(object: any): void {
const url = './app/details?id=' + object.id;
// 过滤掉已关闭的窗口
this.childWindow = this.childWindow.filter(e => !window.closed);
// 查找窗口是否已存在
const winIndex = this.childWindow.findIndex(item =>
item.location !== null && ('.' + item.location.pathname + item.location.search) === url);
if (winIndex !== -1) {
// 将已存在的窗口置前
this.childWindow[winIndex].focus();
this.childWindow[winIndex].resizeTo(1282, 1000);
this.childWindow[winIndex].moveTo(0, 0);
return;
}
// 若不存在则创建一个窗口,并将保存到数组中
const editWindow = window.open(url, '_blank',
'toolbar=no, location=no, directories=no, status=no, ' +
'menubar=no, scrollbars=yes, resizable=no, width=1282, height=1000');
this.childWindow.push(editWindow);
}