使用TypeScript设置window.location

时间:2012-10-28 06:49:22

标签: javascript jquery typescript

我收到以下TypeScript代码的错误:

 ///<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');
        });

 }

我收到以下明确的红色错误:

$link.attr('data-href'); 

消息说:

Cannot convert 'string' to 'Location': Type 'String' is missing property 'reload' from type 'Location'

有谁知道这意味着什么?

2 个答案:

答案 0 :(得分:116)

window.location的类型为Location,而.attr('data-href')返回一个字符串,因此您必须将其分配给window.location.href,这也是字符串类型。为此,请替换以下行:

window.location = $link.attr('data-href');

这个:

window.location.href = $link.attr('data-href');

答案 1 :(得分:15)

您错过了href

标准,使用window.location.href作为window.location在技术上是一个包含以下内容的对象:

Properties
hash 
host 
hostname
href    <--- you need this
pathname (relative to the host)
port 
protocol 
search 

 window.location.href = $link.attr('data-href');