如何重定向到Angular2中的外部URL?

Mic*_*ryl 136 redirect angular2-routing angular

在Angular 2中将用户重定向到完全外部URL的方法是什么.例如,如果我需要将用户重定向到OAuth2服务器以进行身份​​验证,我该怎么做?

Location.go(),Router.navigate()并且Router.navigateByUrl()可以将用户发送到Angular 2应用程序中的另一个部分(路由),但我看不出它们如何用于重定向到外部站点?

Den*_*lek 180

你可以使用这个 - > window.location.href = '...';

这会将页面更改为您想要的任何内容..

  • 请记住,直接引用窗口会将代码限制为具有窗口对象的平台. (7认同)
  • 呃,我出于某种原因尝试将其称为功能,而不仅仅是改变价值.直接设置值有效. (3认同)
  • @ender没有替代品吗?在某些情况下,我们确实希望重定向到外部链接。当然,我们可以使用window.location.href,但正如您所说,它有其缺点。如果Angular中有受支持的API,那会更好。 (2认同)
  • @DennisSmolek但是如果你尝试使用Universal将其编译成一个Android应用程序,这种行为会不会失败? (2认同)

Bri*_*ian 94

前面描述的方法的Angular方法是DOCUMENT@angular/common(或@angular/platform-browser在Angular <4中)导入并使用

document.location.href = 'https://stackoverflow.com';
Run Code Online (Sandbox Code Playgroud)

在一个函数内部.

一些-page.component.ts

import { DOCUMENT } from '@angular/common';
...
constructor(@Inject(DOCUMENT) private document: Document) { }

goToUrl(): void {
    this.document.location.href = 'https://stackoverflow.com';
}
Run Code Online (Sandbox Code Playgroud)

一些-page.component.html

<button type="button" (click)="goToUrl()">Click me!</button>
Run Code Online (Sandbox Code Playgroud)

查看plateformBrowser repo了解更多信息.

  • 在角度4中,DOCUMENT应该从`@angular/common`导入(即'@ {/常见'的'import {DOCUMENT};'),否则这个效果很好!请参阅https://github.com/angular/angular/blob/master/packages/platform-b​​rowser/src/dom/dom_tokens.ts (4认同)
  • 以这种方式注入文档的目的是什么?@Inject(DOCUMENT)私有文档:any` (4认同)
  • @SunilGarg 注入`DOCUMENT` 可以更轻松地在单元测试中用模拟替换文档对象。它还允许在其他平台(服务器/移动设备)上使用替代实现。 (2认同)
  • 对于严格键入,我使用`constructor(@Inject(DOCUMENT)private document:Document){} (2认同)

Mic*_*ryl 36

正如丹尼斯斯莫勒克所说,解决方案很简单.设置window.location.href为您要切换到的URL,它只是工作.

例如,如果组件的类文件(控制器)中有此方法:

goCNN() {
    window.location.href='http://www.cnn.com/';
}
Run Code Online (Sandbox Code Playgroud)

然后你可以通过(click)在模板中的按钮(或其他)上进行适当的调用来简单地调用它:

<button (click)="goCNN()">Go to CNN</button>
Run Code Online (Sandbox Code Playgroud)


aba*_*het 21

我认为你需要àtarget="_ blank",所以你可以使用window.open:

gotoGoogle() : void {
     window.open("https://www.google.com", "_blank");
}
Run Code Online (Sandbox Code Playgroud)


Hen*_*ell 9

如果你一直在使用OnDestry生命周期钩子,你可能有兴趣在调用window.location.href =之前使用这样的东西.

    this.router.ngOnDestroy();
    window.location.href = 'http://www.cnn.com/';
Run Code Online (Sandbox Code Playgroud)

这将触发您可能喜欢的组件中的OnDestry回调.

哦,还有:

import { Router } from '@angular/router';
Run Code Online (Sandbox Code Playgroud)

是你找到路由器的地方.

---编辑---可悲的是,我在上面的例子中可能错了.至少它现在没有像我的生产代码那样工作 - 所以,在我有时间进一步调查之前,我这样解决它(因为我的应用程序确实需要钩子)

this.router.navigate(["/"]).then(result=>{window.location.href = 'http://www.cnn.com/';});
Run Code Online (Sandbox Code Playgroud)

基本上路由到任何(虚拟)路由以强制挂钩,然后按要求导航.

  • 不,不,没有用。太疯狂了 (2认同)

sra*_*nji 8

有 2 个选项:

  1. 如果您想在同一窗口/选项卡中重定向

     gotoExternalDomain(){
         window.location.href='http://google.com/'
     }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 如果你想在新标签中重定向

     gotoExternalDomain(){
         (window as any).open("http://google.com/", "_blank");
     }
    
    Run Code Online (Sandbox Code Playgroud)

  • 你好!您能解释一下“_blank”是什么吗,因为链接在新选项卡中打开时也没有它? (2认同)

小智 5

我使用 Angular 2 Location 来完成此操作,因为我不想自己操作全局窗口对象。

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');
  }
}
Run Code Online (Sandbox Code Playgroud)

  • Angular 2 Location 听起来不适用。文档指出 location 的用途是:“Location 负责根据应用程序的基本 href 规范化 URL。” 使用它来重定向到应用程序内的 URL 可能是合适的;使用它重定向到外部 URL 似乎不符合文档。 (2认同)

Lau*_*hes 5

我用了 window.location.href='http://external-url';

对我来说,重定向在 Chrome 中有效,但在 Firefox 中无效。以下代码解决了我的问题:

window.location.assign('http://external-url');
Run Code Online (Sandbox Code Playgroud)


小智 5

在扯掉我的头之后,解决方案只是将 http:// 添加到 href 。

<a href="http://www.URL.com">Go somewhere</a>
Run Code Online (Sandbox Code Playgroud)

  • 你用这个回答什么问题? (8认同)

use*_*048 5

在较新版本的Angular中,将window作为 any

(window as any).open(someUrl, "_blank");
Run Code Online (Sandbox Code Playgroud)

  • Angular 上的较新版本是否不允许仅使用普通窗口? (2认同)

Sir*_*Ali 5

您可以通过多种方式重定向:

喜欢

window.location.href = 'redirect_url';
Run Code Online (Sandbox Code Playgroud)

另一种方式 Angular 文档:

从 Angular 导入文档,并且该文档必须被注入,否则会出现错误

import { DOCUMENT  } from '@angular/common';
export class AppComponent {
     constructor(
       @Inject(DOCUMENT) private document: Document
    ) {}
   this.document.location.href = 'redirect_url';
}
Run Code Online (Sandbox Code Playgroud)