java窗口添加标签页_在新标签页中打开新窗口

window 变量为 undefined 的原因是 you have declared a variable named window again in the local scope .

根据 javascript/typescript 的范围规则,在访问全局变量之前,查找局部变量值 . 此外,当您最初声明一个变量时,它被设置为undefined,因此您收到的错误消息 .

因此,您只需更改捕获已打开选项卡引用的变量名称即可

var newWindow = window.open('some_url');

然而,这不是推荐的方法,因为angular2应用程序可以在各种环境中运行,例如移动或者呈现服务器端,其中 window 对象可能可用,也可能不可用 . 更不用说在测试中模拟窗口对象会非常困难

相反,您可以将 window 对象包装在服务中并将该服务注入到组件中 . 这样,您可以使用依赖注入简单地根据环境替换服务实现

The service file

@Injectable()

export class WindowRef {

constructor() {}

getNativeWindow() {

return window;

}

}

The component file

@Component({

selector : 'demo',

template : '

Demo
'

})

class DemoComponent {

nativeWindow: any

constructor( private winRef: WindowRef ) {

this.nativeWindow = winRef.getNativeWindow();

}

protected assignActity(type: string): void {

var newWindow = this.nativeWindow.open('/#/link');

this.Service.assignActivity(type).subscribe(res => {

newWindow.location = '/#/link/' + res;

console.log(res);

})

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值