Javascript 使用TypeScript设置window.location
我收到以下TypeScript代码的错误:Javascript 使用TypeScript设置window.location,javascript,jquery,typescript,Javascript,Jquery,Typescript,我收到以下TypeScript代码的错误: ///<reference path='../../../Shared/typescript/jquery.d.ts' /> ///<reference path='../../../Shared/typescript/jqueryStatic.d.ts' /> function accessControls(action: Action) { $('#logoutLink') .click(fu
///<reference path='../../../Shared/typescript/jquery.d.ts' />
///<reference path='../../../Shared/typescript/jqueryStatic.d.ts' />
function accessControls(action: Action) {
$('#logoutLink')
.click(function () {
var $link = $(this);
window.location = $link.attr('data-href');
});
}
消息说:
Cannot convert 'string' to 'Location': Type 'String' is missing property 'reload' from type 'Location'
有人知道这是什么意思吗?
window.location
的类型为location
,而.attr('data-href')
返回一个字符串,因此您必须将其分配给window.location.href
,该类型也是字符串类型。为此,请替换以下行:
window.location = $link.attr('data-href');
对于这一个:
window.location.href = $link.attr('data-href');
您错过了
href
:
标准,将window.location.href
用作window.location
从技术上讲是包含以下内容的对象:
Properties
hash
host
hostname
href <--- you need this
pathname (relative to the host)
port
protocol
search
我注意到,在今天的浏览器中,设置
window.location=“some string”
具有特殊的行为,请参见此处:-查看有关相同站点、相同来源和XHR行为的注释。
Properties
hash
host
hostname
href <--- you need this
pathname (relative to the host)
port
protocol
search
window.location.href = $link.attr('data-href');