ChatGPT解决这个技术问题 Extra ChatGPT

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

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

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

你不觉得这不容易做到很可笑吗?必须有一种更简单的方法来做到这一点,而不必为此编写代码。
注意:如果您的外部 URL 不包含 http:// 或 https://,Angular 4 会将 URL 视为内部路由。以防其他人为此苦苦挣扎。
@aherocallFrog 你的评论拯救了我的一天。谢谢!

Z
Zoe stands with Ukraine

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

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


呃,出于某种原因,我尝试将其作为函数调用,而不仅仅是更改值。直接设置值即可。
请记住,直接引用窗口会将您的代码限制在具有窗口对象的平台上。
@ender 没有替代品吗?可能存在我们真正想要重定向到外部链接的情况。当然,我们可以使用 window.location.href,但就像你说的,它有它的缺点。如果 Angular 中有支持的 API,那会更好。
当然,但这是运行 javascript 并需要进行页面重定向的所有平台的 99%。甚至 phantom/casperJs 也尊重 window 对象。您可以调用 document.location.href 甚至 Location.href,因为它们都引用同一个对象。 Location Reference
@DennisSmolek 但是,如果您尝试使用 Universal 将其编译成一个 android 应用程序,这种行为不会失败吗?
O
Ollie

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

document.location.href = 'https://stackoverflow.com';

在一个函数里面。

一些页面.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 存储库以了解更多信息。


目标站点是否知道请求来自我们的应用程序,因为这里的要求是为了登录而重定向,因此 SSO 应用程序应该在成功后重定向回我们的应用程序
在角度 4 中,应该从 @angular/common(即 import { DOCUMENT } from '@angular/common';)导入 DOCUMENT,否则效果很好!请参阅github.com/angular/angular/blob/master/packages/…
以这种方式注入文档的目的是什么@Inject(DOCUMENT) private document: any
@SunilGarg 注入 DOCUMENT 使得在单元测试中用模拟替换文档对象变得更加容易。它还允许在其他平台(服务器/移动)上使用替代实现。
对于我使用的严格类型,constructor(@Inject(DOCUMENT) private document: Document) { }
C
Community

The solution,正如 Dennis Smolek 所说,非常简单。将 window.location.href 设置为您要切换到的 URL,它就可以正常工作。

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

goCNN() {
    window.location.href='http://www.cnn.com/';
}

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

<button (click)="goCNN()">Go to CNN</button>

J
JustLearning

我认为您需要 à target="_blank",因此您可以使用 window.open

gotoGoogle() : void {
     window.open("https://www.google.com", "_blank");
}

H
Henry Arousell

如果您一直在使用 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/';});

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


非常感谢这个解决方案。我一直在寻找解决方案并设法在新选项卡上打开一个外部链接,但在同一个选项卡上所有 window.open、location.pathname/href.... 并没有让我离开我的网站。这是处理 Angular 应用程序路由的最干净和正确的方法!
W
WizardsOfWor

在较新版本的 Angular 中,窗口作为 any

(window as any).open(someUrl, "_blank");

最佳答案
Angular 上的较新版本是否只允许普通窗口?
J
Joel

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

<a href="http://www.URL.com">Go somewhere</a>

你用这个回答什么问题?
@GuidoFlohr 我有一个表单,人们必须在其中放置链接,有时他们会放置 'www.someLink.com' ,在这种情况下,由于之前缺少 'http',重定向被破坏。所以这个答案是正确的。
@Nitneq问题是关于角度重定向。答案与问题的关系为零。
@GuidoFlohr 有时,Angular 不是问题。
n
naveen

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

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

window.location.assign('http://external-url');

u
user3220080

我是使用 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');
  }
}

对我来说,这只是改变了 URL,应用程序状态没有改变(实际路由看起来相同)。甚至 'location.replaceState('/') 也没有达到我的预期。 router.navigate(['/']); 是为我做的。
Angular 2 Location 听起来不适用。文档指出位置的目的是:“位置负责根据应用程序的基本 href 规范化 URL。”使用它来重定向到应用程序中的 URL 可能是合适的;使用它重定向到外部 URL 似乎不适合文档。
S
Siraj Ali

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

喜欢

window.location.href = 'redirect_url';

Angular 文档的另一种方式:

从角度导入文档,并且必须注入文档以及以下内容,否则您将收到错误消息

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

s
sravan ganji

有2个选项:

如果你想在同一个窗口/标签中重定向 gotoExternalDomain(){ window.location.href='http://google.com/' } 如果你想在新标签中重定向 gotoExternalDomain(){ (window as any).open (“http://google.com/”、“_blank”); }


你好!您能否解释一下 "_blank" 的用途,因为链接在没有它的情况下也在新标签页中打开?
@tz0 它在新选项卡中打开一个 URL。
@Coti,但它也打开了没有“_blank”的网址
R
Rishi0405

以上解决方案都不适合我,我只是添加了

window.location.href = "www.google.com"
event.preventDefault();

这对我有用。

或尝试使用

window.location.replace("www.google.com");

L
Linh

在你的 component.ts

import { Component } from '@angular/core';

@Component({
  ...
})
export class AppComponent {
  ...
  goToSpecificUrl(url): void {
    window.location.href=url;
  }

  gotoGoogle() : void {
    window.location.href='https://www.google.com';
  }
}

在你的 component.html

<button type="button" (click)="goToSpecificUrl('http://stackoverflow.com/')">Open URL</button>
<button type="button" (click)="gotoGoogle()">Open Google</button>

<li *ngFor="item of itemList" (click)="goToSpecificUrl(item.link)"> // (click) don't enable pointer when we hover so we should enable it by using css like: **cursor: pointer;**

A
Akitha_MJ

就这么简单

window.location.href='http://www.google.com/';

这在过去几年中已经多次说过。