Nice programing

TypeScript로 window.location 설정

nicepro 2020. 12. 3. 19:41
반응형

TypeScript로 window.location 설정


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

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


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

당신은 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');

참고 URL : https://stackoverflow.com/questions/13106950/set-window-location-with-typescript

반응형