在 TypeScript 中创建 Location 对象

Creating a Location object in TypeScript

我无法将 Angular 之外的应用程序重定向到注销页面。 我通过使用 $window.location.href 来完成它,但它不适用于 FireFox。 因此,有人建议直接使用 $window.location,但由于我是用 Typescript 编写的,所以我需要创建一个新的 Location 对象,而不是将我的字符串分配给它...

我查看了 lib.d.ts,发现该位置声明为:

declare var Location: {
    prototype: Location;
    new(): Location;
}

所以我将代码调整为:

var url: string = "http:\\host:port/blabla/logout";
var loc: Location = new Location();
loc.href = url;
this.$window.location = loc;

但出现此错误:

Error: Illegal constructor.

知道如何创建 Location 对象吗?这样做是一种好习惯吗? 还有其他见解吗?

谢谢

如果你想直接分配一个字符串并让 TypeScript 与之配合,你可以这样做

 window.location = <any>"http://host:port/blabla/logout";

看起来你实际上是 not supposed to build new Location objects. So I don't think there's a constructor available anywhere for this. Though you can use anchors to build them for you

如果您尝试设置新位置,可以设置 window.location.href 而不是 window.location (These two are equivalent)。