2017-07-03 74 views
0

我想在页面启动时打开一个新的页面,但我不断收到一个角4 NgOnInitReference错误

Reference Error: Window not defined.

我只开始与打字稿及Angular4最近的工作,所以我不这样确定这个问题真正属于哪一个。

export class HomeComponent implements OnInit { 

testCall: Window; 

constructor() {  
    this.testCall = new Window(); 
    this.testCall.open(
    "https://static.pexels.com/photos/104827/cat-pet-animal-domestic-104827.jpeg"); 
} 

ngOnInit(): void { 
    console.log("function read"); 

    window.open("https://static.pexels.com/photos/104827/cat-pet-animal-domestic-104827.jpeg"); 
} 

即使我使用this.testCall.openOnInit()里面,我只得到一个参考错误,但在open方法。

我知道这是一个简单的页面打开时,第一页打开,但它已经一个星期了,我真的不知道如何在打字稿/ angular4设置中做到这一点。

回答

0

删除此行

this.testCall = new Window(); 

window是JS的全局变量,只是打电话window.open('url')应该打开一个新的窗口。

+0

已经尝试过,没有工作。窗口未定义。 :( – RoboKy

+0

你需要删除该行,如果邮件仍然存在,那么你并没有删除它 – trichetriche

+0

当然,我删除了该行,除了现在它给我“TypeError:无法读取未定义的属性”打开“真的很抱歉对于这个问题 – RoboKy

0

您是否尝试在包含HomeComponent?的文件中声明window对象。如果不是,请尝试将其声明为;

declare var window: any; 

位于文件顶部(导入后)。

TypeScript不知道窗口,因为我们没有提供任何关于该窗口的输入信息。在这种情况下,我们可以声明那些告诉打字稿编译器全局命名空间中的window引用不是源自打字稿文件。

了解更多关于此方案here

0

好吧显然窗口不存在的服务器,这就是为什么当我尝试把“window.open”有

ngOnInit()和构造函数()引发错误
import { Component, PLATFORM_ID, Inject, OnInit } from '@angular/core'; 
import { isPlatformBrowser } from '@angular/common'; 

export class HomeComponent { 
submitted: boolean = false; 

constructor(@Inject(PLATFORM_ID) private platformId: Object) { 

} 

ngOnInit() { 
    if (isPlatformBrowser(this.platformId)) { 
     window.open("URL"); 
    } 
} 
} 

这对我来说非常合适。