programing

글로벌 변수를 내부 모듈로 호출합니다.

codeshow 2023. 3. 23. 22:58
반응형

글로벌 변수를 내부 모듈로 호출합니다.

제겐 타자기본 파일이 있어요Projects.ts부트스트랩 플러그인에서 선언된 글로벌 변수를 참조할 수 있습니다.bootbox.js.

라는 변수에 접속하고 싶다.bootboxTypeScript 클래스 내에서.

가능합니까?

컴파일러에 선언되었음을 알려야 합니다.

declare var bootbox: any;

더 나은 유형의 정보가 있는 경우 다음 대신 해당 정보를 추가할 수 있습니다.any.

아직 모르시는 분들을 위해서...declare외부 진술class바로 다음과 같습니다.

declare var Chart: any;

@Component({
  selector: 'my-component',
  templateUrl: './my-component.component.html',
  styleUrls: ['./my-component.component.scss']
})

export class MyComponent {
    //you can use Chart now and compiler wont complain
    private color = Chart.color;
}

TypeScriptdeclare 키워드는 declare에서 발신되지 않을 수 있는 변수를 정의하는 경우에 사용합니다.TypeScript파일.

컴파일러에게 이 변수가 런타임에 값을 갖는다는 것을 알고 있기 때문에 컴파일 오류를 발생시키지 말라고 말하는 것과 같습니다.

참조는 하지만 변이가 없는 경우const:

declare const bootbox;

Sohnee 솔루션이 더 깔끔하지만,

window["bootbox"]

프로젝트 전체에 걸쳐 이 변수에 대한 참조를 가지려면 어딘가에 작성하십시오.d.ts파일(예:globals.d.ts. 글로벌 변수 선언을 입력합니다.다음은 예를 들어 다음과 같습니다.

declare const BootBox: 'boot' | 'box';

프로젝트 전체에서 다음과 같이 참조할 수 있습니다.

const bootbox = BootBox;

여기 예가 있습니다.

// global.d.ts
declare global {
  namespace NodeJS {
    interface Global {
      bootbox: string; // Specify ur type here,use `string` for brief
    }
  }
}

// somewhere else
const bootbox = global.bootbox
// somewhere else
global.bootbox = 'boom'

부트 박스 타이핑 다운로드

그런 다음 .ts 파일 내에 참조를 추가합니다.

언급URL : https://stackoverflow.com/questions/13252225/call-a-global-variable-inside-module

반응형