我收到以下打字脚本代码错误:

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

 }

I am getting an underlined red error for the following:

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

The message says:

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

有人知道这意味着什么吗?

推荐答案

window.location is of type Location while .attr('data-href') returns a string, so you have to assign it to window.location.href which is of string type too. For that replace your following line:

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

for this one:

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

Jquery相关问答推荐

我需要在上一个表格单元格中显示计算结果

如何使用 jquery 获取屏幕的高度

元素上的 jQuery Change 事件 - 有什么方法可以保留以前的值?

如何使用jQuery删除父元素

jquery如何捕获输入键并将事件更改为选项卡

什么是不使用 jQuery 的经验技术原因?

将变量传递给jQuery AJAX成功回调中的函数

jQuery Mobile:动态添加内容的标记增强

Twitter Bootstrap alert 可以淡入淡出吗?

为什么 Chrome 会忽略本地 jQuery cookie?

如何将参数传递给 JavaScript 中的匿名函数?

隐藏 div 但保留空白

Bootstrap 折叠动画不流畅

Rails 5:如何将 $(document).ready() 与 turbo-links 一起使用

$(document).on("click"... 不工作?

jQuery - 从元素内部 Select 元素

如何插入元素作为第一个子元素?

如何禁用由子元素触发的 mouseout 事件?

jQuery向左滑动并显示

如何使用 jQuery 的 form.serialize 但排除空字段