109

我收到以下 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'

有谁知道这意味着什么?

4

3 回答 3

207

window.locationis 类型Locationwhile.attr('data-href')返回一个字符串,所以你必须将它分配给它window.location.href也是字符串类型。为此替换您的以下行:

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

对于这个:

window.location.href = $link.attr('data-href');
于 2012-10-28T06:50:48.373 回答
37

你错过了href

标准,在技术上使用window.location.hrefas是一个包含以下内容的对象: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');
于 2012-10-28T06:53:17.050 回答
-4

只需添加href

像这样:

window.location.href = $link.attr('data-href');
于 2021-09-20T07:25:19.347 回答