TypeScript로 window.location 설정


85

다음 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'

이것이 무엇을 의미하는지 아는 사람이 있습니까?

답변:


164

window.locationis of type Locationwhile .attr('data-href')returns a string, so you must assign it to window.location.hrefwhich is of string type. 이를 위해 다음 줄을 바꿉니다.

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

이것에 대해 :

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

1
오늘날 브라우저에서 설정 window.location = "some string"에는 특별한 동작이 있습니다. 여기를 참조하십시오. stackoverflow.com/questions/2383401/…- 동일 사이트, 동일 출처 및 XHR 동작에 대한 주석을 참조하십시오.
다이

27

당신은 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');
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.