在Angular 2中,将用户重定向到完全外部的URL的方法是什么?例如,如果我需要重定向用户到OAuth2服务器以进行身份验证,我该如何做?
Location.go()
、Router.navigate()
和Router.navigateByUrl()
可以用于将用户发送到Angular 2应用程序中的另一个部分(路由),但我看不出它们如何用于重定向到外部网站?
在Angular 2中,将用户重定向到完全外部的URL的方法是什么?例如,如果我需要重定向用户到OAuth2服务器以进行身份验证,我该如何做?
Location.go()
、Router.navigate()
和Router.navigateByUrl()
可以用于将用户发送到Angular 2应用程序中的另一个部分(路由),但我看不出它们如何用于重定向到外部网站?
window.location.href = '...';
来改变页面到你想要的任何位置。document.location.href
或甚至Location.href
,因为它们都指向同一个对象。 位置引用 - Dennis Smolek@angular/common
(或在 Angular < 4 中使用 @angular/platform-browser
)中的 DOCUMENT
,并使用它来实现先前描述的方法。document.location.href = 'https://stackoverflow.com';
在一个函数内部。
some-page.component.ts
import { DOCUMENT } from '@angular/common';
...
constructor(@Inject(DOCUMENT) private document: Document) { }
goToUrl(): void {
this.document.location.href = 'https://stackoverflow.com';
}
some-page.component.html
<button type="button" (click)="goToUrl()">Click me!</button>
请查看platformBrowser代码库以获取更多信息。
@angular/common
导入 DOCUMENT(例如 import { DOCUMENT } from '@angular/common';
),否则这个功能非常好用!请参见 https://github.com/angular/angular/blob/master/packages/platform-browser/src/dom/dom_tokens.ts。 - John@Inject(DOCUMENT) private document: any
有什么目的? - Sunil Gargconstructor(@Inject(DOCUMENT) private document: Document) { }
。 - Ian Jamieson解决方案正如Dennis Smolek所说,是非常简单的。将window.location.href
设置为您要切换到的URL,它就可以正常工作。
例如,如果您在组件类文件(控制器)中有以下方法:
goCNN() {
window.location.href='http://www.cnn.com/';
}
然后在模板中使用相应的 (click)
调用,你可以很简单地通过一个按钮(或其他任何元素)来完成:
<button (click)="goCNN()">Go to CNN</button>
我认为你需要添加目标属性为 "_blank" ,这样你就可以使用window.open
:
gotoGoogle() : void {
window.open("https://www.google.com", "_blank");
}
如果你一直在使用OnDestry生命周期钩子,那么在调用window.location.href=...之前,你可能会对像这样的内容感兴趣。
this.router.ngOnDestroy();
window.location.href = 'http://www.cnn.com/';
这将会触发你组件中的OnDestry回调函数,这可能是你所喜欢的。
哦,还有:
import { Router } from '@angular/router';
这里是你找到路由器的地方。
---编辑--- 不幸的是,我可能在上面的示例中错了。至少现在在我的生产代码中没有按预期工作 - 因此,直到我有时间进一步调查,我会像这样解决它(因为我的应用程序真的需要尽可能使用钩子)
this.router.navigate(["/"]).then(result=>{window.location.href = 'http://www.cnn.com/';});
基本上是将路由定向到任何(虚拟的)路由以触发钩子,然后按请求导航。
在较新版本的Angular中,使用any
作为窗口对象。
(window as any).open(someUrl, "_blank");
有两个选项:
if you want to redirect in same window/tab
gotoExternalDomain(){
window.location.href='http://google.com/'
}
if you want to redirect in new tab
gotoExternalDomain(){
(window as any).open("http://google.com/", "_blank");
}
"_blank"
是什么意思吗?因为即使没有它,链接也可以在新标签页中打开。 - tz0<a href="http://www.URL.com">Go somewhere</a>
https://angular.io/docs/ts/latest/api/common/index/Location-class.html#!#prepareExternalUrl-anchor
可以这样做:import {Component} from '@angular/core';
import {Location} from '@angular/common';
@Component({selector: 'app-component'})
class AppCmp {
constructor(location: Location) {
location.go('/foo');
}
}
window.location.href = 'redirect_url';
另一种 Angular 文档方式:
从 Angular 中导入 document,必须注入文档,如下所示,否则将会出现错误。
import { DOCUMENT } from '@angular/common';
export class AppComponent {
constructor(
@Inject(DOCUMENT) private document: Document
) {}
this.document.location.href = 'redirect_url';
}