我已经在我的应用程序中使用 ngx-zxing 模块在我的离子角应用程序中实现了 QR 扫描仪。
扫描后,我想将用户重定向到扫描链接直接重定向的页面,而无需用户手动干预。
代码.html
<!-- code for qr scanner alreday implemented, the result is present in the below code -->
<div>
<a href="{{qrResult}}" #link></a>
</div>
代码.ts
//all imports and components are written.I am directly implementing the function definition in in which I have to redirect to the link.
handleQrCodeResult(result: string) {
console.log("Result", result);
this.qrResult = result;
this.clickLink();
}
clickLink(){
let el = (<HTMLImageElement>document.getElementById('link'))
console.log('el', el); // this is returning null.
el.click();
}
看一看,下面的代码不需要人工交互
handleQrCodeResult(result: string) {
console.log("Result", result);
this.qrResult = result;
// This line will redirect to the link you want
window.location.href = "Insert link here"; // Maybe: document.getElementById('link')
}
解释
-
window.location.href = "URL";
如果将 url 分配给此变量,浏览器将重定向到同一页面中的 url,不会打开新选项卡 -
window.open("URL", "_blank");
使用它在新选项卡中打开网址