我想打开一个新的弹出窗口,一旦 URL 加载到新的弹出窗口中,我就想关闭它。
这是我的组件
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
title = 'app';
ngOnInit():void{
var popup=window.open('https://www.w3schools.com','newwindow','width=300,height=200');
popup.onload=function(){
alert("Hello");
window.close();
}
}
}
在上面的代码中
popup.onload=function(){
alert("Hello");
window.close();
}
不工作,没有警报,也没有关闭窗口。 但如果我只是做
popup.alert("Hello");
popup.close();
它起作用了。但是我想在加载整个 URL 时关闭窗口,即在 onload() 函数上。
请提供有关此问题的一些指示。
提前致谢!
最佳答案
如果可以,为什么要使用 onLoad 你使用 ngAfterViewInit()。 “在 Angular 初始化组件的 View 和 subview /指令所在的 View 后响应。”
popup: any;
ngOnInit():void{
this.popup=window.open('https://www.w3schools.com','newwindow','width=300,height=200');
}
ngAfterViewInit() {
this.popup.close();
}
导入 ngAfterViewInit 并尝试这是否适合您。
关于javascript - window.onload 在 Angular 4 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49665264/